<template>
  <el-container>
    <el-header v-if="isSearch">
      <el-form style="margin: 10px 0" :inline="true">
        <el-form-item label="演习类型：">
          <el-input v-model="drillType" placeholder="请输入演习类型" />
        </el-form-item>
        <el-form-item>
          <el-button
            style="margin-left: 10px"
            type="primary"
            icon="el-icon-search"
            @click="getList"
          >
            搜索
          </el-button>
          <el-button class="commonBtn" icon="el-icon-refresh" @click="reset"> 重置 </el-button>
        </el-form-item>
      </el-form>
    </el-header>
    <el-main>
      <div class="headerTop">
        <div>
          <el-button
            type="primary"
            class="el-icon-plus"
            @click="add"
          >
            新增</el-button
          >
        </div>
        <div>
          <i class="commonSearch el-icon-refresh-left"></i>
          <i
            class="commonSearch el-icon-search"
            circle
            @click="isSearch = !isSearch"
          ></i>
        </div>
      </div>
      <el-table
        height="calc(100% - 200px)"
        :data="list"
        :row-style="{ height: '0px' }"
        :header-cell-style="{ 'text-align': 'center', padding: '0px' }"
        :cell-style="{ 'text-align': 'center', padding: '2px' }"
        style="width: 100%"
      >
        <el-table-column label="序号" align="center">
          <template slot-scope="scope">
            <span> {{ (pageNum - 1) * pageSize + (scope.$index + 1) }}</span>
          </template>
        </el-table-column>

        <el-table-column label="演习类型" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.drillType }}</span>
          </template>
        </el-table-column>
        <el-table-column label="修改时间" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.updateDate }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center" width="300">
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="edit(scope.row.id)">
              编辑
            </el-button>
            <el-button type="text" size="mini" @click="del(scope.row.id)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :page.sync="pageNum"
        :limit.sync="pageSize"
        :page-sizes="[10, 20, 30, 50]"
        :total="total"
        @pagination="getList"
      />
    </el-main>
    <el-dialog
      :visible.sync="addAlert"
      width="30%"
      :title="title"
      :show-close="false"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form
        ref="addForm"
        :model="addForm"
        label-position="right"
        label-width="130px"
        inline
        :rules="rules1"
        style="overflow: auto"
      >
        <el-form-item label="演习类型：" prop="drillType">
          <el-input
            v-model="addForm.drillType"
            placeholder="请输入演习类型"
          />
        </el-form-item>
      </el-form>
      <el-row
        style="
          text-align: right;
          margin-top: 50px;
          padding-top: 10px;
          border-top: 1px solid rgb(33, 67, 91);
        "
      >
        <el-button type="primary" @click="addSubmit('addForm')">
          提交
        </el-button>
        <el-button class="abrogateBtn" @click="addCancel"> 取消 </el-button>
      </el-row>
    </el-dialog>
  </el-container>
</template>

<script>
import { getList, getInfo, configuration, configurations, remove } from "@/api/emergencies";
export default {
  name: "RuoyiAllocation",

  data() {
    return {
      addAlert: false,
      isSearch: false,
      pageNum: 1,
      pageSize: 10,
      total: 1,
      list: [],
      addForm: {
        drillType: "",
      },
      title: '添加演习类型',
      rules1: {
        drillType: [{ required: true, message: "请输入演习类型", trigger: "blur" }],
      },
      drillType: ''
    };
  },

  mounted() {
    this.getList()
  },

  methods: {
    add() {
      this.addAlert = true
      this.title = '添加演习类型'
    },
    edit(id) {
      this.title = '编辑演习类型'
      getInfo({ id }).then(res => {
        if (res.code === 200) {
          this.addForm = res.data
          this.addAlert = true
        }
      })
    },
    del(id) {
      this.$confirm("确认删除这条演习类型吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async() => {
           remove(id).then(res => {
            if(res.code === 200) {
               if (this.list.length === 1 && this.pageNum > 1) {
            this.pageNum -= 1;
          }
          this.getList();
          this.$message.success(res.msg);
            }
           })

        })
        .catch(() => {
          this.$message.error("已取消删除");
        });
    },
    getList() {
      getList({drillType: this.drillType, pageNum:this.pageNum, pageSize: this.pageSize}).then((res) => {
        if (res.code === 200) {
          this.list = res.rows
          this.total = res.total
        }
          console.log(res)

      })
    },
    addSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.title === '添加演习类型') {
            configuration({ drillType: this.addForm.drillType}).then(res => {
              if(res.code === 200) {
                this.$message.success(res.msg);
                this.addAlert = false;
                this.getList()
              }
            })
          } else {
            configurations({ drillType: this.addForm.drillType, id: this.addForm.id}).then(res => {
              if(res.code === 200) {
                this.$message.success(res.msg);
                this.addAlert = false;
                this.getList()
              }
            })
          }
        } else {
          return false;
        }
      });
    },
    reset() {
      this.drillType = ''
      this.pageNum = 1
      this.pageSize = 10
      this.getList()
    },
    addCancel() {
      this.addAlert = false;
      this.addForm = {};
      this.$refs.addForm.resetFields();
    },
  },
};
</script>

<style lang="scss" scoped></style>
