<template>
  <div class="container">
    <!-- 查询配置 -->
    <div style="padding: 0px 20px">
      <div ref="lxHeader">
        <LxHeader icon="md-apps" text="应急演练记录" style="margin-bottom: 10px; border: 0px; margin-top: 15px">
          <div slot="content"></div>
          <slot>
            <div>
              <Input style="width: 200px; margin-right: 10px" v-model="searchFields.KeyWord" placeholder="关键字搜索" />
              <Button type="primary" @click="load" style="margin-left: 10px">查询</Button>
              <Button type="primary" @click="edit()" style="margin-left: 10px">新增</Button>
            </div>
          </slot>
        </LxHeader>
      </div>
      <div ref="lxTable">
        <el-table :data="tableData" :height="tableHeight" style="width: 100%">
          <el-table-column type="index" width="50" align="center"> </el-table-column>
          <el-table-column :prop="column.field" :align="column.align" v-for="(column,index) in columns"
            :label="column.title" :width="column.width">
            <template slot-scope="scope">
              <div v-if="column.type=='opt'">
                <Button ghost size="small" @click="edit(scope.row)" type="primary"
                  style="margin-left: 10px;">编辑</Button>
                <Button ghost size="small" @click="del(scope.row)" type="error" style="margin-left: 10px;">删除</Button>
              </div>
              <div v-else-if="column.type=='level'" v-for="item in parameters.practiseLevel">
                <div v-if='scope.row[column.field]==item.id'>
                  {{item.value}}
                </div>
              </div>
              <div v-else>{{scope.row[column.field]}}</div>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination">
          <el-pagination @current-change="handleCurrentChange" :current-page="paginations.page"
            :page-size="paginations.page_size" background layout="prev, pager, next" :total="paginations.total">
          </el-pagination>
        </div>
      </div>

    </div>

    <el-dialog title="应急演练记录编辑" :visible.sync="dialogFormVisible" fullscreen width="90%">
      <div class="dialogConcent" :style="{height:clientHeight+'px'}">
        <el-scrollbar style="flex: 1">
          <el-form :model="form" :rules="rules" ref="form" label-position="right" :label-width="formLabelWidth">
            <el-row>
              <el-col :span="8">
                <el-form-item label="演练运维管理单位" prop="practise_unit" class="units">
                  <el-select v-model="form.practise_unit" placeholder="请选择科室,运维管理单位" style="width: 100%">
                    <el-option v-for="item in deptOptions" :key="item.id" :label="item.name" :value="item.name">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="演练级别" prop="level" label-position="right">
                  <el-select v-model="form.level" placeholder="请选择演练级别" style="width: 100%">
                    <el-option v-for="item in parameters.practiseLevel" :key="item.id" :label="item.value"
                      :value="item.id">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8" :offset="8">
                <el-form-item label="演练时间" prop="date">
                  <el-date-picker style="width:100%"
                        v-model="form.date"
                        type="datetime"
                        placeholder="选择演练时间"
                        format="yyyy-MM-dd HH:mm"
                        value-format="yyyy-MM-dd HH:mm">
                      </el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item label="演练地点" prop="address" label-position="right">
              <el-input v-model="form.address" placeholder="请填写演练地点" autocomplete="off"></el-input>
            </el-form-item>
            <el-row>
              <el-col :span="8">
                <el-form-item label="组织运维管理单位" prop="organize_unit" class="units">
                  <el-select v-model="form.organize_unit" placeholder="请选择组织运维管理单位" style="width: 100%">
                    <el-option v-for="item in deptOptions" :key="item.id" :label="item.name" :value="item.id">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8" :offset="8">
                <el-form-item label="演练负责人" prop="responsible_person" class="">
                  <el-select v-model="form.responsible_person" placeholder="请选择演练负责人" style="width: 100%">
                    <el-option v-for="item in userOptions" :key="item.id" :label="item.name" :value="item.id">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item label="参加部门和运维管理单位" prop="joins_list" class="units" label-position="right">
              <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选
              </el-checkbox>
              <div style="margin: 15px 0;"></div>
              <el-checkbox-group v-model="form.joins_list" @change="handleCheckedDeptChange">
                <el-checkbox v-for="dept in deptOptions" :label="dept.id" :key="dept.id">{{dept.name}}</el-checkbox>
              </el-checkbox-group>
            </el-form-item>
            <el-form-item label="演练过程和描述" prop="introduce">
              <el-input type="textarea" :rows="3" v-model="form.introduce" placeholder="请填写演练过程和描述" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="附件" prop="file_list">
              <el-upload class="upload-demo" :on-success="handlesuccess" :data="uploadOther"
                action="/api/admin/upload-file" :on-remove="handleRemove" :before-remove="beforeRemove"
                :on-exceed="handleExceed" :file-list="fileList">
                <el-button size="small" type="primary">点击上传</el-button>
              </el-upload>
            </el-form-item>
            <div style="padding: 20px 0px;" class="form-sub-title">
              <span>评估
              </span>
            </div>
            <div class="form-sub-title form-sub-title-lab">
              <span>预案评估</span>
              <el-row>
                <el-col :span="8">
                  <el-form-item label="适应性" prop="evaluate_plan_suit">
                    <el-radio v-for="item in this.evaluate.evaluate_plan_suit" v-model="form.evaluate_plan_suit" :label="item.id">{{item.value}}</el-radio>
                  </el-form-item>
                </el-col>
                <el-col :span="8" :offset="8">
                  <el-form-item label="充分性" prop="evaluate_plan_full">
                    <el-radio v-for="item in this.evaluate.evaluate_plan_full" v-model="form.evaluate_plan_full" :label="item.id">{{item.value}}</el-radio>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
            <div class="form-sub-title form-sub-title-lab">
              <span>演练评估</span>
              <el-row>
                <el-col :span="8">
                  <el-form-item label="参加人员" prop="evaluate_practise_join">
                    <el-radio v-for="item in this.evaluate.evaluate_practise_join" v-model="form.evaluate_practise_join" :label="item.id">{{item.value}}</el-radio>
                  </el-form-item>
                </el-col>
                <el-col :span="8" :offset="8">
                  <el-form-item label="现场物资" prop="evaluate_practise_material">
                    <el-radio v-for="item in this.evaluate.evaluate_practise_material" v-model="form.evaluate_practise_material" :label="item.id">{{item.value}}</el-radio>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="个人防护" prop="evaluate_practise_protect">
                    <el-radio v-for="item in this.evaluate.evaluate_practise_protect" v-model="form.evaluate_practise_protect" :label="item.id">{{item.value}}</el-radio>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
            <div class="form-sub-title-lab form-sub-title">
              <span>指挥评估</span>
              <el-row>
                <el-col :span="8">
                  <el-form-item label="整体组织指挥" prop="evaluate_direct_whole">
                    <el-radio v-for="item in this.evaluate.evaluate_direct_whole" v-model="form.evaluate_direct_whole" :label="item.id">{{item.value}}</el-radio>
                  </el-form-item>
                </el-col>
                <el-col :span="8" :offset="8">
                  <el-form-item label="各抢险分工" prop="evaluate_direct_divide">
                    <el-radio v-for="item in this.evaluate.evaluate_direct_divide" v-model="form.evaluate_direct_divide" :label="item.id">{{item.value}}</el-radio>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
            <div class="form-sub-title" style="padding:20px 0">
              <span>协作评估
              </span>
            </div>
            <el-form-item label="报告上级" prop="evaluate_cooperate_report">
              <el-radio v-for="item in this.evaluate.evaluate_cooperate_report" v-model="form.evaluate_cooperate_report" :label="item.id">{{item.value}}</el-radio>
            </el-form-item>
            <el-form-item label="消防科室" prop="evaluate_cooperate_fire">
              <el-radio v-for="item in this.evaluate.evaluate_cooperate_fire" v-model="form.evaluate_cooperate_fire" :label="item.id">{{item.value}}</el-radio>
            </el-form-item>
            <el-form-item label="医疗救援科室" prop="evaluate_cooperate_medical">
              <el-radio v-for="item in this.evaluate.evaluate_cooperate_medical" v-model="form.evaluate_cooperate_medical" :label="item.id">{{item.value}}</el-radio>
            </el-form-item>
            <el-form-item label="周边政府配合" prop="evaluate_cooperate_government">
              <el-radio v-for="item in this.evaluate.evaluate_cooperate_government" v-model="form.evaluate_cooperate_government" :label="item.id">{{item.value}}</el-radio>
            </el-form-item>
            <el-form-item label="其他参与运维管理单位" class="units" prop="evaluate_cooperate_other">
              <el-radio v-for="item in this.evaluate.evaluate_cooperate_other" v-model="form.evaluate_cooperate_other" :label="item.id">{{item.value}}</el-radio>
            </el-form-item>
            <el-form-item label="总体评价" prop="evaluate_cooperate_full">
              <el-radio v-for="item in this.evaluate.evaluate_cooperate_full" v-model="form.evaluate_cooperate_full" :label="item.id">{{item.value}}</el-radio>
            </el-form-item>




          </el-form>
        </el-scrollbar>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('form')">取 消</el-button>
        <el-button type="primary" v-preventReClick @click="submitForm('form')">确 定</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
  import {
    getToken
  } from '@/utils/auth'
  import LxHeader from "@/components/LxHeader/index.vue";
  import Tinymce from '@/components/Tinymce'
  import {
    save,
    store,
    del,
    get,
    list
  } from "../../api/practise/index.js";
  import {
    getparameteritem
  } from "../../api/system/dictionary.js";
  import {
    listdept
  } from "../../api/system/department.js";
  import {
    listuser
  } from "../../api/system/user.js";
  export default {
    components: {
      LxHeader,
      Tinymce
    },
    data() {
      return {
        parameters: {
          practiseLevel: [],
        },
        paginations: {
          page: 1,
          page_size: 15,
          total: 0
        },
        checkAll:false,
        isIndeterminate: true,
        tableHeight: 0,
        dialogFormVisible: false,
        formLabelWidth: "120px",
        clientHeight: 0,
        form: {
          practise_unit: "",
          level: "",
          date: "",
          address: "",
          organize_unit: "",
          responsible_person: "",
          introduce: "",
          evaluate_plan_suit: 1,
          evaluate_plan_full:1,
          evaluate_practise_join:1,
          evaluate_practise_material: 1,
          evaluate_practise_protect: 1,
          evaluate_direct_whole: 1,
          evaluate_direct_divide: 1,
          evaluate_cooperate_report: 1,
          evaluate_cooperate_fire: 1,
          evaluate_cooperate_medical: 1,
          evaluate_cooperate_government: 1,
          evaluate_cooperate_other: 1,
          evaluate_cooperate_full: 1,
          file_list:"",
          joins_list:[]
        },

        fileList: [],
        tableData: [],
        rules: {
          practise_unit: [{
            required: true,
            message: '请选择演练运维管理单位',
            trigger: 'blur'
          }],
          level: [{
            required: true,
            message: '请选择演练级别',
            trigger: 'blur'
          }],
          organize_unit: [{
            required: true,
            message: '请选择组织运维管理单位',
            trigger: 'blur'
          }]
        },
        tableHeight: 900,
        searchFields: {
          KeyWord: ""
        },
        columns: [{
            field: "level",
            title: "演练级别",
            type: "level",
            align:"left",
            width: 180
          },
          {
            field: "date",
            title: "演练时间",
            type: "string",
            width: 180,
            align:"center",
          },
          {
            field: "address",
            title: "演练地点",
            type: "string",
            align:"left"
          },
          {
            field: "practise_unit",
            title: "演练运维管理单位",
            type: "unit",
            width: 200,
            align:"center",
          },
          {
            field: "introduce",
            title: "描述",
            type: "string",
          },
          {
            field: "created_at",
            title: "创建信息",
            type: "string",
            width: 180,
            align: "center"
          },
          {
            field: "操作",
            title: "操作",
            width: 220,
            type: "opt",
          }
        ],
        //查询条件字段
        searchFields: {
          KeyWord: ""
        },
        uploadOther: {
          token: ""
        },
        deptOptions: [],
        userOptions: [],
        evaluate:{
          evaluate_plan_suit:[
            {
              id:1,
              value:"全部能够执行"
            },
            {
              id:2,
              value:"执行过程不够顺利"
            },
            {
              id:3,
              value:"不适宜"
            }
          ],
          evaluate_plan_full:[
            {
              id:1,
              value:"能满足应急要求"
            },
            {
              id:2,
              value:"基本满足"
            },
            {
              id:3,
              value:"必须修改"
            }
          ],
          evaluate_practise_join:[
            {
              id:1,
              value:"好"
            },
            {
              id:2,
              value:"较好"
            },
            {
              id:3,
              value:"基本到位"
            },{
              id:4,
              value:"不到位"
            }
          ],
          evaluate_practise_material:[
            {
              id:1,
              value:"好"
            },
            {
              id:2,
              value:"较好"
            },
            {
              id:3,
              value:"基本到位"
            },{
              id:4,
              value:"不到位"
            }
          ],
          evaluate_practise_protect:[
            {
              id:1,
              value:"好"
            },
            {
              id:2,
              value:"较好"
            },
            {
              id:3,
              value:"基本到位"
            },{
              id:4,
              value:"不到位"
            }
          ],
          evaluate_direct_whole:[
            {
              id:1,
              value:"好"
            },
            {
              id:2,
              value:"较好"
            },
            {
              id:3,
              value:"基本到位"
            },{
              id:4,
              value:"不到位"
            }
          ],
          evaluate_direct_divide:[
            {
              id:1,
              value:"好"
            },
            {
              id:2,
              value:"较好"
            },
            {
              id:3,
              value:"基本到位"
            },{
              id:4,
              value:"不到位"
            }
          ],
          evaluate_cooperate_report:[
            {
              id:1,
              value:"报告及时"
            },
            {
              id:2,
              value:"基本到位"
            },
            {
              id:3,
              value:"联系不上"
            }
          ],
          evaluate_cooperate_fire:[
            {
              id:1,
              value:"按照要求协作"
            },
            {
              id:2,
              value:"基本到位"
            },
            {
              id:3,
              value:"行动迟缓"
            }
          ],
          evaluate_cooperate_medical:[
            {
              id:1,
              value:"按照要求协作"
            },
            {
              id:2,
              value:"基本到位"
            },
            {
              id:3,
              value:"行动迟缓"
            }
          ],
          evaluate_cooperate_government:[
            {
              id:1,
              value:"按照要求协作"
            },
            {
              id:2,
              value:"基本到位"
            },
            {
              id:3,
              value:"不配合"
            }
          ],
          evaluate_cooperate_other:[
            {
              id:1,
              value:"报告及时"
            },
            {
              id:2,
              value:"基本到位"
            },
            {
              id:3,
              value:"不配合"
            }
          ],
          evaluate_cooperate_full:[
            {
              id:1,
              value:"优秀"
            },
            {
              id:2,
              value:"良好"
            },
            {
              id:3,
              value:"基本合格"
            },
            {
              id:4,
              value:"不合格"
            }
          ]
        }
      }
    },
    created() {
      this.uploadOther.token = getToken();
      this.loadDeptOptions();
      this.loadUserOptions();
      this.initLoad();
      this.load();

    },
    methods: {
      loadDeptOptions() {
        listdept().
        then((res) => {
          this.deptOptions = res;
        }).catch(error => {
          console.log(error)
          reject(error)
        })
      },
      loadUserOptions() {
        listuser().
        then((res) => {
          this.userOptions = res.data;
        }).catch(error => {
          console.log(error)
          reject(error)
        })
      },
      initLoad() {
        var that = this;
        var clientHeight = document.documentElement.clientHeight
        var lxHeader_height = 96.5; //查询 头部
        var paginationHeight = 37; //分页的高度
        var topHeight = 50; //页面 头部
        let tableHeight = clientHeight - lxHeader_height - topHeight - paginationHeight - 20;
        that.tableHeight = tableHeight;
        //加载自定义参数
        getparameteritem("practiseLevel").then(res => {
          this.parameters.practiseLevel = res.detail;
        });
      },
      handleCurrentChange(page) {
        this.paginations.page = page;
        this.load();
      },
      load() {
        list({
          page: this.paginations.page,
          page_size: this.paginations.page_size,
        }).then(response => {
          this.tableData = response.data;
          this.paginations.total = response.total;
        }).catch(error => {
          console.log(error)
          reject(error)
        })

      },
      del(obj) {
        var that = this;
        if (obj) {
          this.$Modal.confirm({
            title: '确认要删除数据？',
            onOk: () => {
              del(obj.id).then(response => {
                this.$Message.success('操作成功');
                that.load();
              }).catch(error => {
                console.log(error)
                reject(error)
              })
            },
            onCancel: () => {
              //this.$Message.info('Clicked cancel');
            }
          });
        }
      },
      info(obj) {
        var that = this;
        get(obj.id).then(res => {
          let result = Object.assign(that.form, res);
          that.form = result;
          let _files = [];
          for (var mod of result.practise_files) {
            let m = Object.assign({}, mod["files"]);
            m.name = mod["files"].original_name;
            _files.push(m);
          }
          that.fileList = _files;
          for (var m of result.joins) {
            if (m.department_id)
              that.form.joins_list.push(m.department_id)
          }
        }).catch(error => {
          //reject(error)
        })
      },
      edit(obj) {
        this.form = this.$options.data().form;
        this.clientHeight = document.documentElement.clientHeight - 84 - 110;
        if (obj) {
          var that = this;
          that.info(obj);
        } else {}
        this.dialogFormVisible = true;
      },
      submitForm(formName) {
        var that = this;
        var arrs = [];
            for (var m of this.form.joins_list) {
              arrs.push({
                department_id: m
              })
            }
            this.form.joins_list = arrs;
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (that.form.id) {
              that.form.id = that.form.id;
              save(that.form).then(response => {
                //console.log(response)
                this.$Message.success('操作成功');
                that.load();
                that.dialogFormVisible = false;

              }).catch(error => {
                //reject(error)
              })
            } else {
              store(that.form).then(response => {
                //console.log(response)
                this.$Message.success('操作成功');
                that.load();
                that.dialogFormVisible = false;
              }).catch(error => {
                //reject(error)
              })
            }
            that.fileList = [];
          } else {
            this.$Message.error('数据校验失败');
            console.log('error submit!!');
            return false;
          }
        });
      },
      handleRemove(file, fileList) {
        let listUrl = [];
        for (var m of fileList) {
          if (m.response)
            listUrl.push({"upload_id":m.response.id});
          else
            listUrl.push({"upload_id":m.id});
        }
        this.form.file_list = listUrl;
      },
      handleExceed(files, fileList) {},
      beforeRemove(file, fileList) {
        return this.$confirm(`确定移除 ${ file.name }？`);
      },
      handlesuccess(response, file, fileList) {
        let listUrl = [];
        for (var m of fileList) {
          if (m.response)
            listUrl.push({"upload_id":m.response.id});
          else
            listUrl.push({"upload_id":m.id});

        }
        this.form.file_list = listUrl;
      },
      handleCheckAllChange(val) {
        console.log(val)
        let options = [];
        for (var m of this.deptOptions) {
          options.push({
            department_id:m.id
          });
        }
        this.form.joins_list = val ? options : [];
        this.isIndeterminate = false;
      },
      handleCheckedDeptChange(value) {
        let checkedCount = value.length;
        this.checkAll = checkedCount === this.deptOptions.length;
        this.isIndeterminate = checkedCount > 0 && checkedCount < this.deptOptions.length;
      },
      resetForm(formName) {
        var that = this;
        that.fileList = [];
        this.$refs[formName].resetFields();
        that.dialogFormVisible = false;
      },
    }
  }
</script>

<style>
  .dialogConcent {
    overflow-y: auto;
  }
  .units label{
    line-height: 22px;
  }
  .form-sub-title-lab{
    padding: 20px;border:1px solid #ccc;margin-top:10px
  }
</style>
