<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="演练名称" prop="drillName">
        <el-input
          v-model="queryParams.drillName"
          placeholder="请输入演练名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="演练类型" prop="drillType">
        <el-select v-model="queryParams.drillType" placeholder="请选择演练类型" clearable size="small">
          <el-option
            v-for="dict in drillTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="演练形式" prop="drillForm">
        <el-select v-model="queryParams.drillForm" placeholder="请选择演练形式" clearable size="small">
          <el-option
            v-for="dict in drillFormOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:drill:add']"
        >新增</el-button>
      </el-col>
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="success"-->
          <!--plain-->
          <!--icon="el-icon-edit"-->
          <!--size="mini"-->
          <!--:disabled="single"-->
          <!--@click="handleUpdate"-->
          <!--v-hasPermi="['system:drill:edit']"-->
        <!--&gt;修改</el-button>-->
      <!--</el-col>-->
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="danger"-->
          <!--plain-->
          <!--icon="el-icon-delete"-->
          <!--size="mini"-->
          <!--:disabled="multiple"-->
          <!--@click="handleDelete"-->
          <!--v-hasPermi="['system:drill:remove']"-->
        <!--&gt;删除</el-button>-->
      <!--</el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:drill:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="drillList" @selection-change="handleSelectionChange">
      <!--<el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="演练名称" align="center" prop="drillName" />
      <el-table-column label="演练类型" align="center" prop="drillType" :formatter="drillTypeFormat" />
      <el-table-column label="演练形式" align="center" prop="drillForm" :formatter="drillFormFormat" />
      <el-table-column label="演练地址" align="center" prop="drillAddress" />
      <el-table-column label="主办单位" align="center" prop="drillUnit" />
      <el-table-column label="参演总人数" align="center" prop="numberDrillers" />
      <el-table-column label="演练时间" align="center" prop="drillTime" width="180"/>
      <!--<el-table-column label="演练目的" align="center" prop="drillObjective" />-->
      <!--<el-table-column label="参演人员" align="center" prop="drillPeople" />-->
      <!--<el-table-column label="演练内容" align="center" prop="drillContent" />-->
      <!--<el-table-column label="评估" align="center" prop="assessment" />-->
      <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>
      <el-table-column  label="状态" align="center" prop="evaluate" >
        <span slot-scope="scope" v-if="scope.row.evaluate">已完成</span>
        <span v-else>待评估</span>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document-copy"
            @click="handleDetail(scope.row)"
            v-hasPermi="['system:drill:edit']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit-outline"
            v-if="!scope.row.evaluate"
            @click="assessment(scope.row)"
          >评估</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:drill:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改应急演练对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1100px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <div class="division" style="margin-bottom:20px" >

          <div class="div-kuang" style="width: 50%;">
            <el-form-item label="演练名称" prop="drillName">
              <el-input v-model="form.drillName" placeholder="请输入演练名称" />
            </el-form-item>
            <el-form-item label="演练类型" prop="drillType">
              <el-select v-model="form.drillType" placeholder="请选择演练类型">
                <el-option
                  v-for="dict in drillTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="演练形式" prop="drillForm">
              <el-select v-model="form.drillForm" placeholder="请选择演练形式">
                <el-option
                  v-for="dict in drillFormOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="演练地址" prop="drillAddress">
              <el-input v-model="form.drillAddress" placeholder="请输入演练地址" />
            </el-form-item>
            <el-form-item label="演练时间" prop="drillTime" >
              <el-date-picker clearable size="small"
                              style="width: 100%"
                              v-model="form.drillTime"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="选择演练时间">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="主办单位" prop="drillUnit">
              <el-input v-model="form.drillUnit" placeholder="请输入主办单位" />
            </el-form-item>
            <el-form-item label="演练目的" prop="drillObjective">
              <el-input type="textarea" v-model="form.drillObjective" placeholder="请输入演练目的" />
            </el-form-item>
          </div>

          <div class="div-kuang" style="width: 58%;margin-left: 2%">
<!--            <el-form-item label="参演人员" prop="drillPeople">-->
<!--              <el-input v-model="form.drillPeople" type="textarea" placeholder="请输入内容" />-->
<!--            </el-form-item>-->
            <el-form-item label="演练内容">
              <editor v-model="form.drillContent" :min-height="240"/>
            </el-form-item>
          </div>

        </div>

        <div class="div-kuang" style="width: 100%;">
          <el-form-item label="选择人员" prop="releaseTime">
            <!-- table -->
            <!-- jsonSelectNameList就是呗选中的人员的json -->
            <!-- getPeopleList 是每次选中或者删除人员都会返回 一个所有人员列表的json串，[{staffId:staffId,staffName:staffName},{staffId:staffId,staffName:staffName}] -->
            <!-- 要在jsonSelectNameList赋值完毕之后 调用一下 this.$refs.changePaple.changeNameList  135行 -->
            <ChangePapel
              ref="changePaple"
              :jsonSelectNameList="jsonSelectNameList"
              @getPeopleList="getPeopleList"
            />
          </el-form-item>
        </div>


        <!--<el-form-item label="评估" prop="assessment">-->
          <!--<el-input v-model="form.assessment" type="textarea" placeholder="请输入内容" />-->
        <!--</el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 详情 -->
    <el-dialog title="演练详情" :visible.sync="open2" width="1200px" append-to-body>
      <el-form  label-width="90px" size="mini">
        <div class="division">
          <div class="div-kuang" style="width: 40%;">
            <el-form-item label="演练名称:" prop="drillName">
              <span>{{form.drillName}}</span>
            </el-form-item>
            <el-form-item label="演练类型:" prop="drillType">
              <span>{{form.drillType}}</span>
            </el-form-item>
            <el-form-item label="演练形式:" prop="drillForm">
              <span>{{form.drillForm}}</span>
            </el-form-item>
            <el-form-item label="演练地址:" prop="drillAddress">
              <span>{{form.drillAddress}}</span>
            </el-form-item>
            <el-form-item label="演练时间:" prop="drillTime" >
              <span>{{form.drillTime}}</span>
            </el-form-item>
            <el-form-item label="主办单位:" prop="drillUnit">
              <span>{{form.drillUnit}}</span>
            </el-form-item>
            <el-form-item label="演练目的:" prop="drillObjective">
              <span>{{form.drillObjective}}</span>
            </el-form-item>
            <el-form-item label="参演人数:" prop="drillObjective">
              <span>{{form.numberDrillers}}</span>
            </el-form-item>
          </div>
          <div class="div-kuang" style="width: 58%;margin-left: 2%">
            <el-form-item label="参演人员:" prop="drillPeople">
              <span style="margin-right:5px" v-for='item in form.jsonPeople' :key="item.peoPleId">{{item.peoPleName}}</span>
            </el-form-item>
            <el-form-item label="演练内容:">
              <editor v-model="form.drillContent" :min-height="240" :readOnly="readOnly"/>
            </el-form-item>
          </div>
          <div class="div-kuang" style="width: 50%;margin-left: 2%">
          <el-form-item label="总结评价:" >
            <span>{{form.evaluate}}</span>
          </el-form-item>
          <el-form-item label="整改措施:">
            <span>{{form.measures}}</span>
          </el-form-item>
          </div>
        </div>


        <!--<el-form-item label="评估" prop="assessment">-->
        <!--<el-input v-model="form.assessment" type="textarea" placeholder="请输入内容" />-->
        <!--</el-form-item>-->
      </el-form>
    </el-dialog>
    <!--评估-->
    <el-dialog title="评估" :visible.sync="dialogFormVisible">
      <el-form  ref="form" :model="form">
        <el-form-item label="演练效果和总结评价:" >
          <el-input  maxlength="2000" v-model="form.evaluate"  type="textarea" :rows="4" autocomplete="off" show-word-limit></el-input>
        </el-form-item>
        <el-form-item label="演练存在的问题及整改措施:" >
          <el-input   maxlength="2000" v-model="form.measures" type="textarea" :rows="4" autocomplete="off" show-word-limit></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="dialogFormVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import ChangePapel from "@/components/PeopleChange";
import { listDrill, getDrill, delDrill, addDrill, updateDrill, exportDrill } from "@/api/system/drill";
import { TStaffList } from "@/api/safetyManagement/staff";
import Editor from '@/components/Editor';
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Drill",
  components: {
    Editor,
    ChangePapel
  },
  data() {
    return {
      // 参考人员
      jsonSelectNameList: null,
      //参演人员
      data: [],
      //参演人数
      numberPersonnel:null,
      value: [],
      //选中下标
      check:[],
      filterMethod(query, item) {
        return item.pinyin.indexOf(query) > -1;
      },
      test:null,
      // 遮罩层
      loading: true,
      readOnly:true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 应急演练表格数据
      drillList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2:false,
      //评估
      dialogFormVisible:false,
      // 演练类型字典
      drillTypeOptions: [],
      // 演练形式字典
      drillFormOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        drillName: null,
        drillType: null,
        drillForm: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        drillName: [
          { required: true, message: "演练名称不能为空", trigger: "blur" }
        ],
        drillType: [
          { required: true, message: "演练类型不能为空", trigger: "blur" }
        ],
        drillForm: [
          { required: true, message: "演练形式不能为空", trigger: "blur" }
        ],
        drillAddress: [
          { required: true, message: "演练地址不能为空", trigger: "blur" }
        ],
        drillTime: [
          { required: true, message: "演练时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    if (this.bankId) {
      this.getLessonById();
    }
    this.getList();
    this.getDicts("t_drill_type").then(response => {
      this.drillTypeOptions = response.data;
    });
    this.getDicts("t_drill_form").then(response => {
      this.drillFormOptions = response.data;
    });
  },
  mounted() {
  },
  methods: {
    // 获取参考人员的list
    getPeopleList(list) {
      //参演人员数量
      //this.numberPersonnel=JSON.parse(list).length
      //参演人员
      //this.data=list;
      this.form.drillPeople=list;
      this.form.numberDrillers=JSON.parse(list).length
    },
    // 复现
    getLessonById() {
      getBank(this.bankId).then((res) => {
        console.log("res", res);
        if (res.code == 200) {
          this.form = {
            bankName: res.data.bankName,
            deptId: res.data.deptId,
          };
        }
      });
    },
    /**穿梭框数据查询*/
    generateData(){
      const data = [];
      TStaffList(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.jsonSelectNameList=response;
        this.$nextTick(()=>{
          this.$refs.changePaple.changeNameList(this.jsonSelectNameList);
        })
        // response.rows.forEach((city, index) => {
        //   data.push({
        //     label: city.staffName,
        //     key: index,
        //     pinyin: city.staffName
        // });
        //   })
      });

      this.data=data;
    },
    /**参演人员选中信息方法  获取数组下标*/
    handleChange(value, direction, movedKeys) {
      this.check=value;
    },
    /** 查询应急演练列表 */
    getList() {
      this.loading = true;
      listDrill(this.queryParams).then(response => {
        this.drillList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 演练类型字典翻译
    drillTypeFormat(row, column) {
      return this.selectDictLabel(this.drillTypeOptions, row.drillType);
    },
    // 演练形式字典翻译
    drillFormFormat(row, column) {
      return this.selectDictLabel(this.drillFormOptions, row.drillForm);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        drillId: null,
        drillName: null,
        drillAddress: null,
        drillUnit: null,
        drillTime: null,
        drillType: null,
        drillForm: null,
        drillObjective: null,
        drillPeople: null,
        drillContent: null,
        assessment: null,
        createTime: null,
        createBy: null,
        isDel: null,
        evaluate:null,
        measures:null,
        numberDrillers:0,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.drillId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // listStaff(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
      //   console.log(response.rows)
      //   this.cities = response.rows.staffName;
      //   generateData.cities=['shanghai', 'beijing', 'guangzhou', 'shenzhen', 'nanjing', 'xian', 'chengdu']
      // });
       this.generateData();
      this.reset();
      this.open = true;
      this.title = "添加应急演练";
      this.$nextTick(()=>{
        this.$refs.changePaple.changeNameList(this.jsonSelectNameList);
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const drillId = row.drillId || this.ids
      getDrill(drillId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改应急演练";
        console.log(response.data.drillPeople)
        this.jsonSelectNameList=response.data.drillPeople;
        this.$nextTick(()=>{
          this.$refs.changePaple.changeNameList(this.jsonSelectNameList);
        })
      });
    },
    /** 评估按钮操作 */
    assessment(row) {
      this.reset();
      const drillId = row.drillId || this.ids
      getDrill(drillId).then(response => {
        this.form = response.data;
        this.dialogFormVisible = true;
      });
    },
    /*评估提交*/
    submitAssessment(){

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.drillId != null) {
            updateDrill(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.dialogFormVisible=false;
              this.getList();
            });
          } else {
            addDrill(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      getDrill(row.drillId).then(response => {
        this.form = response.data;
        this.form.jsonPeople = JSON.parse(this.form.drillPeople)
        console.log(this.form.jsonPeople)
        this.form.drillType = this.selectDictLabel(this.drillTypeOptions, row.drillType);
        this.form.drillForm = this.selectDictLabel(this.drillFormOptions, row.drillForm);
        this.open2 = true;
    });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      //const drillIds = row.drillId || this.ids;
      this.$confirm('是否确认删除数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return updateDrill({drillId:row.drillId,isDel:1});
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有应急演练数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportDrill(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
<style>
  .div-kuang{
    background: white;
    padding-top:20px ;
    padding-right: 20px;
    padding-bottom:10px;
    border-radius: 10px;
  }
  .division{
    display:flex;
    flex-direction:row;
    justify-content:flex-start;
  }
</style>
<style lang="scss" scoped>
  ::v-deep .el-select{
    width: 100%;
  }
  ::v-deep .el-dialog__header{
    border-bottom: 1px solid #ccc;
  }
  ::v-deep .el-dialog{
    background: #f7f7f7;
  }
</style>
