<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="search" style="margin-left: 10px">查询</Button>
              <Button type="primary" @click="edit()" style="margin-left: 10px">新增</Button>
            </div>
          </slot>
        </LxHeader>
      </div>
      <div ref="lxTable" class="table-tree">
        <el-table :data="tableData" class="v-table" :height="tableHeight" style="width: 100%">
          <el-table-column type="index" fixed width="50" align="center"> </el-table-column>
          <el-table-column :prop="column.field" :fixed="column.fixed" :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=='unit'">
                <div v-for="item in deptOptions">
                  <div v-if='scope.row[column.field]==item.id'>
                    {{item.name}}
                  </div>
                </div>

              </div>
              <div v-else-if="column.type=='formats'">
                <div v-if="column.field=='admin'||column.field=='department'||column.field=='maintenance_department'">
                  {{scope.row[column.field]?scope.row[column.field].name:""}}
                </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="unit">
                  <el-select v-model="form.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="year">
                  <el-date-picker style="width:100%" type="year" v-model="form.year" value-format="yyyy"
                    placeholder="请选择年份">
                  </el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item class="longtext" label="目标职责运行质量" prop="duty">
              <el-input type="textarea" v-model="form.duty" :rows="3" placeholder="请填写目标职责运行质量" autocomplete="off">
              </el-input>
            </el-form-item>
            <el-form-item class="longtext">
              <el-upload class="upload-demo" :on-success="handlesuccess" :data="uploadOther"
                :action="action" :on-remove="handleRemove" :before-remove="beforeRemove"
                :on-exceed="handleExceed" :file-list="fileList.duty_files">
                <el-button size="small" type="primary" @click="toupload('duty_files_list')">点击上传</el-button>
              </el-upload>
            </el-form-item>
            <el-form-item class="longtext" label="制度化管理运行质量" prop="regulation">
              <el-input type="textarea" v-model="form.regulation" :rows="3" placeholder="请填写制度化管理运行质量" autocomplete="off">
              </el-input>
            </el-form-item>
            <el-form-item class="longtext">
              <el-upload class="upload-demo" :on-success="handlesuccess" :data="uploadOther"
                :action="action" :on-remove="handleRemove" :before-remove="beforeRemove"
                :on-exceed="handleExceed" :file-list="fileList.regulation_files">
                <el-button size="small" type="primary" @click="toupload('regulation_files_list')">点击上传</el-button>
              </el-upload>
            </el-form-item>
            <el-form-item class="longtext" label="教育培训运行质量" prop="education">
              <el-input type="textarea" v-model="form.education" :rows="3" placeholder="请填写教育培训运行质量" autocomplete="off">
              </el-input>
            </el-form-item>
            <el-form-item class="longtext">
              <el-upload class="upload-demo" :on-success="handlesuccess" :data="uploadOther"
                :action="action" :on-remove="handleRemove" :before-remove="beforeRemove"
                :on-exceed="handleExceed" :file-list="fileList.education_files">
                <el-button size="small" type="primary" @click="toupload('education_files_list')">点击上传</el-button>
              </el-upload>
            </el-form-item>

            <el-form-item class="longtext" label="现场管理运行质量" prop="scene">
              <el-input type="textarea" v-model="form.scene" :rows="3" placeholder="请填写现场管理运行质量" autocomplete="off">
              </el-input>
            </el-form-item>
            <el-form-item class="longtext">
              <el-upload class="upload-demo" :on-success="handlesuccess" :data="uploadOther"
                :action="action" :on-remove="handleRemove" :before-remove="beforeRemove"
                :on-exceed="handleExceed" :file-list="fileList.scene_files">
                <el-button size="small" type="primary" @click="toupload('scene_files_list')">点击上传</el-button>
              </el-upload>
            </el-form-item>

            <el-form-item class="longtext" label="安全风险管控及隐患排查治理运行质量" prop="risk">
              <el-input type="textarea" v-model="form.risk" :rows="3" placeholder="请填写安全风险管控及隐患排查治理运行质量" autocomplete="off">
              </el-input>
            </el-form-item>
            <el-form-item class="longtext">
              <el-upload class="upload-demo" :on-success="handlesuccess" :data="uploadOther"
                :action="action" :on-remove="handleRemove" :before-remove="beforeRemove"
                :on-exceed="handleExceed" :file-list="fileList.risk_files">
                <el-button size="small" type="primary" @click="toupload('risk_files_list')">点击上传</el-button>
              </el-upload>
            </el-form-item>

            <el-form-item class="longtext" label="应急管理运行质量" prop="measure">
              <el-input type="textarea" v-model="form.measure" :rows="3" placeholder="请填写应急管理运行质量" autocomplete="off">
              </el-input>
            </el-form-item>
            <el-form-item class="longtext">
              <el-upload class="upload-demo" :on-success="handlesuccess" :data="uploadOther"
                :action="action" :on-remove="handleRemove" :before-remove="beforeRemove"
                :on-exceed="handleExceed" :file-list="fileList.measure_files">
                <el-button size="small" type="primary" @click="toupload('measure_files_list')">点击上传</el-button>
              </el-upload>
            </el-form-item>

            <el-form-item class="longtext" label="事故管理运行质量" prop="accident">
              <el-input type="textarea" v-model="form.accident" :rows="3" placeholder="请填写事故管理运行质量" autocomplete="off">
              </el-input>
            </el-form-item>
            <el-form-item class="longtext">
              <el-upload class="upload-demo" :on-success="handlesuccess" :data="uploadOther"
                :action="action" :on-remove="handleRemove" :before-remove="beforeRemove"
                :on-exceed="handleExceed" :file-list="fileList.accident_files">
                <el-button size="small" type="primary" @click="toupload('accident_files_list')">点击上传</el-button>
              </el-upload>
            </el-form-item>

            <el-form-item class="longtext" label="持续改进运行质量" prop="continue">
              <el-input type="textarea" v-model="form.continue" :rows="3" placeholder="请填写持续改进运行质量" autocomplete="off">
              </el-input>
            </el-form-item>
            <el-form-item class="longtext">
              <el-upload class="upload-demo" :on-success="handlesuccess" :data="uploadOther"
                :action="action" :on-remove="handleRemove" :before-remove="beforeRemove"
                :on-exceed="handleExceed" :file-list="fileList.continue_files">
                <el-button size="small" type="primary" @click="toupload('continue_files_list')">点击上传</el-button>
              </el-upload>
            </el-form-item>

            <el-form-item class="longtext" label="安全生产标准化总体运行质量" prop="evaluation">
              <el-input type="textarea" v-model="form.evaluation" :rows="3" placeholder="请填写安全生产标准化总体运行质量" autocomplete="off">
              </el-input>
            </el-form-item>
            <el-form-item class="longtext">
              <el-upload class="upload-demo" :on-success="handlesuccess" :data="uploadOther"
                :action="action" :on-remove="handleRemove" :before-remove="beforeRemove"
                :on-exceed="handleExceed" :file-list="fileList.evaluation_files">
                <el-button size="small" type="primary" @click="toupload('evaluation_files_list')">点击上传</el-button>
              </el-upload>
            </el-form-item>

            <el-form-item class="longtext" label="备注" prop="remark">
              <el-input type="textarea" v-model="form.remark" :rows="3" placeholder="请填写备注" autocomplete="off">
              </el-input>
            </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 {
    getparameteritem
  } from "../../api/system/dictionary.js"
  import {
    save,
    store,
    del,
    get,
    list
  } from "../../api/improvement/score.js";
  import {
    listdept
  } from "../../api/system/department.js"
  export default {
    components: {
      LxHeader,
      Tinymce
    },
    data() {
      return {      action:`${process.env.VUE_APP_BASE_API}api/admin/upload-file`,
        touploadType: "",
        checkAll: false,
        isIndeterminate: true,
        parameters: {
          accidentType: [],
          accidentLevel: []
        },
        paginations: {
          page: 1,
          page_size: 15,
          total: 0
        },

        tableHeight: 0,
        dialogFormVisible: false,
        formLabelWidth: "120px",
        clientHeight: 0,
        form: {
          unit: "",
          year: "",
          remark: "",
          duty: "",
          regulation: "",
          education: "",
          scene: "",
          risk: "",
          measure:"",
          accident:"",
          continue:"",
          evaluation:"",

          duty_files_list:[],
          regulation_files_list:[],
          education_files_list:[],
          scene_files_list:[],
          risk_files_list:[],
          measure_files_list:[],
          accident_files_list:[],
          continue_files_list:[],
          evaluation_files_list:[],

        },
        fileList: {
          duty_files:[],
          regulation_files:[],
          education_files:[],
          scene_files:[],
          risk_files:[],
          measure_files:[],
          accident_files:[],
          continue_files:[],
          evaluation_files:[],
        },
        tableData: [],
        rules: {
          unit: [{
            required: true,
            message: '请选择科室',
            trigger: 'blur'
          }],
          year: [{
            required: true,
            message: '请选择年份',
            trigger: 'blur'
          }],
        },
        tableHeight: 900,
        searchFields: {
          KeyWord: ""
        },
        columns: [{
            field: "year",
            title: "年份",
            type: "string",
            width: 120,
            align: "left"
          },
          {
            field: "remark",
            title: "备注",
            type: "string",
          },
          {
            field: "unit",
            title: "科室",
            type: "unit",
            align: "center",
            width: 120,
          },
          {
            field: "created_at",
            title: "创建信息",
            type: "string",
            width: 180,
            align: "center"
          },
          {
            field: "admin",
            title: "创建人",
            type: "formats",
            width: 120,
            align: "center"
          },
          {
            field: "department",
            title: "创建人科室",
            type: "formats",
            width: 120,
            align: "center"
          },
          {
            field: "操作",
            title: "操作",
            width: 220,
            type: "opt",
          }
        ],
        uploadOther: {
          token: ""
        },
        deptOptions: [],
        all_files: [],
      }
    },
    created() {
      this.uploadOther.token = getToken();
      this.loadDeptOptions();
      this.initLoad();
      this.load();

    },
    methods: {
      toupload(type) {
        console.log(type)
        this.touploadType = type;
      },
      loadDeptOptions() {
        listdept().
        then((res) => {
          this.deptOptions = res;
        }).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;

      },
      handleCurrentChange(page) {
        this.paginations.page = page;
        this.load();
      },
      search() {
        this.paginations.page = 1;
        this.load();
      },
      load() {
        list({
          page: this.paginations.page,
          page_size: this.paginations.page_size,
          keyword:this.searchFields.KeyWord
        }).then(res => {
          this.tableData = res.data;
          this.paginations.total = res.total
        }).catch(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');
            }
          });
        }
      },
      show(obj) {
        this.clientHeight = document.documentElement.clientHeight - 84 - 110;
        this.dialogViewVisible = true;
        this.info(obj);
      },
      info(obj) {
        var that = this;
        get(obj.id).then(res => {
          let result = Object.assign(that.form, res);
          that.form = result;
          Object.keys(this.fileList).map(key => {
            if (result[key].length != 0) {
              let _files = [];
              for (var mod of result[key]) {
                let m = Object.assign({}, mod.files);
                m.name = mod.files.original_name;
                _files.push(m);
                m.touploadType = key;
                that.all_files.push(m);
              }
              that.fileList[key] = _files;

            }

          })


        }).catch(error => {
          //reject(error)
        })
      },
      edit(obj) {
        var that = this;
        this.form = this.$options.data().form;

        that.fileList.duty_files=[];
        that.fileList.regulation_files=[];
        that.fileList.education_files=[];
        that.fileList.scene_files=[];
        that.fileList.risk_files=[];
        that.fileList.measure_files=[];
        that.fileList.accident_files=[];
        that.fileList.continue_files=[];
        that.fileList.evaluation_files=[];
        this.clientHeight = document.documentElement.clientHeight - 84 - 110;
        if (obj) {

          that.info(obj);
        } else {}
        this.dialogFormVisible = true;
      },
      submitForm(formName) {
        var that = this;
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (that.form.id) {
              save(that.form).then(response => {
                //console.log(response)
                this.$Message.success('操作成功');
                that.load();
                that.dialogFormVisible = false;

              }).catch(error => {
                //reject(error)
              })
            } else {
              // console.log("that.form",that.form);
              // return;
              store(that.form).then(response => {
                //console.log(response)
                this.$Message.success('操作成功');
                that.load();
                that.dialogFormVisible = false;
              }).catch(error => {
                //reject(error)
              })
            }
           that.fileList.duty_files=[];
           that.fileList.regulation_files=[];
           that.fileList.education_files=[];
           that.fileList.scene_files=[];
           that.fileList.risk_files=[];
           that.fileList.measure_files=[];
           that.fileList.accident_files=[];
           that.fileList.continue_files=[];
           that.fileList.evaluation_files=[];
          } else {
            this.$Message.error('数据校验失败');
            console.log('error submit!!');
            return false;
          }
        });
      },
      handleRemove(file, fileList) {
        var _touploadType = this.all_files.filter((x) => {
          return x.uid == file.uid;
        })[0].touploadType;

        console.log(_touploadType);
        var type = 1;
        let listUrl = [];
        for (var m of fileList) {
          if (m.response)
            listUrl.push({
              upload_id: m.response.id,
              type: type
            });
          else
            listUrl.push({
              upload_id: m.id,
              type: type
            });
        }
        this.form[_touploadType] = listUrl;
      },
      handleExceed(files, fileList) {},
      beforeRemove(file, fileList) {
        return this.$confirm(`确定移除 ${ file.name }？`);
      },
      handlesuccess(response, file, fileList) {
        //console.log(response,file,fileList);
        let listUrl = [];
        var type = 1;

        Object.keys(this.fileList).map((key, index) => {
          if (this.touploadType == key)
            type = index + 1
        })
        for (var m of fileList) {
          if (m.response)
            listUrl.push({
              upload_id: m.response.id,
              type: type
            });
          else
            listUrl.push({
              upload_id: m.id,
              type: type
            });
        }
        file.touploadType = this.touploadType;
        this.all_files.push(file);
        this.form[this.touploadType] = listUrl;
      },
      resetForm(formName) {
        var that = this;
        that.fileList.duty_files=[];
        that.fileList.regulation_files=[];
        that.fileList.education_files=[];
        that.fileList.scene_files=[];
        that.fileList.risk_files=[];
        that.fileList.measure_files=[];
        that.fileList.accident_files=[];
        that.fileList.continue_files=[];
        that.fileList.evaluation_files=[];
        this.$refs[formName].resetFields();
        that.dialogFormVisible = false;
      },
    }
  }
</script>

<style>
  .dialogConcent {
    overflow-y: auto;
  }

  .units label {
    line-height: 22px;
  }
  .longtext{
    margin-left:70px;
  }
  .longtext label{
    float:none;
    text-align: left;
  }
  .longtext .el-form-item__content{
    margin-left:0!important;
  }
</style>
