<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="模板名称" prop="temName">
        <el-input
          v-model="queryParams.temName"
          placeholder="请输入模板名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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="['kaohe:template: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="['kaohe:template:edit']"
        >修改</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="['kaohe:template:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['kaohe:template:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="templateList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="模板名称" align="center" prop="temName" />
      <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-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['kaohe:template:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['kaohe:template: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="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="模板名称" prop="temName">
          <el-input v-model="form.temName" placeholder="请输入模板名称" />
        </el-form-item>
        <el-form-item label="考核项">
          <el-button type="primary" @click="selectKhitem" size="mini">选择考核项</el-button>
          <el-table :data="form.items" style="width: 100%; margin-top: 10px" border max-height="500">
            <el-table-column label="操作" width="100" fixed align="center">
              <template slot-scope="scope">
                <i class="el-icon-remove-outline" style="font-size: 20px;color:#F56C6C;" @click="delKhitemList(scope.row,scope.$index)"></i>
              </template>
            </el-table-column>
            <el-table-column label="主键" align="center" prop="id" />
            <el-table-column label="考核项名称" align="center" prop="itemName" />
          </el-table>
        </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="khitem.open" width="1200px" append-to-body>
      <div style="display:flex;width: 100%;justify-content: space-between">
        <div style="width: 40%;">
          <div style="height: 45px;line-height: 45px;font-size: 14px;font-weight: bolder">
            选中考核项 <span style="color: #39CCCC">{{ khitem.selectList.length }}</span>
          </div>
          <el-table :data="khitem.selectList" style="width: 100%" border max-height="500">
            <el-table-column label="操作" width="50" fixed align="center">
              <template slot-scope="scope">
                <i class="el-icon-remove-outline" style="font-size: 20px;color:#F56C6C;" @click="selectListDel(scope.row,scope.$index)"></i>
              </template>
            </el-table-column>
            <el-table-column label="主键" align="center" prop="id" />
            <el-table-column label="考核项名称" align="center" prop="itemName" />
          </el-table>
        </div>
        <div style="width: 59%;">
          <el-form :model="khitem.queryParams" :inline="true" label-width="90px">
            <el-form-item label="考核项名称" prop="itemName" style="margin-bottom: 10px">
              <el-input
                v-model="khitem.queryParams.itemName"
                placeholder="请输入考核项名称"
                clearable
                @keyup.enter.native="khitemQuery"
              />
            </el-form-item>
            <el-form-item style="margin-bottom: 10px">
              <el-button type="primary" icon="el-icon-search" size="small" @click="khitemQuery">搜索</el-button>
            </el-form-item>
          </el-form>
          <el-table border ref="khitemTable" v-loading="khitem.loading" :data="khitem.list" @select="khitemSelect" @select-all="khitemSelectAll">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="主键" align="center" prop="id" />
            <el-table-column label="考核项名称" align="center" prop="itemName" />
            <el-table-column label="考核类型" align="center" prop="type" />
            <el-table-column label="备注" align="center" prop="remark" />
          </el-table>
          <pagination
            v-show="khitem.total>0"
            :total="khitem.total"
            :page.sync="khitem.queryParams.pageNum"
            :limit.sync="khitem.queryParams.pageSize"
            @pagination="khitemQuery"
          />
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="khitemConfirm">确 认</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listTemplate, getTemplate, delTemplate, addTemplate, updateTemplate } from "@/api/kaohe/template"
import { listItems } from "@/api/kaohe/items"

export default {
  name: "Template",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考核模板表格数据
      templateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        temName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        temName: [
          { required: true, message: "模板名称不能为空", trigger: "blur" }
        ]
      },
      /** 选择考核项*/
      khitem: {
        // 是否显示弹出层
        open: false,
        // 列表遮罩层
        loading: true,
        // 列表查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          itemName: undefined
        },
        // 表格数据
        list: [],
        // 选中表格数据
        selectList: [],
        // 总条数
        total: 0,
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询考核模板列表 */
    getList() {
      this.loading = true
      listTemplate(this.queryParams).then(response => {
        this.templateList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        temName: null,
        items: []
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加考核模板"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getTemplate(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改考核模板"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTemplate(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addTemplate(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除考核模板编号为"' + ids + '"的数据项？').then(function() {
        return delTemplate(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('kaohe/template/export', {
        ...this.queryParams
      }, `template_${new Date().getTime()}.xlsx`)
    },
    // 选择考核项
    selectKhitem(){
      this.khitem.open = true;
      this.khitem.selectList = [...this.form.items]
      this.khitem.queryParams.pageNum = 1;
      this.khitemQuery();
    },
    /** 考核项列表查询 */
    khitemQuery() {
      this.khitem.loading = true;
      listItems(this.khitem.queryParams).then(response => {
        this.khitem.list = response.rows;
        this.khitem.total = response.total;
        this.khitem.loading = false;
        this.$nextTick(()=> {
          this.khitem.selectList.forEach(item =>{
            this.khitem.list.forEach(row => {
              if(row.id == item.id){
                this.$refs.khitemTable.toggleRowSelection(row,true);
              }
            });
          })
        })
      });
    },
    /** 考核项选择弹出框 确认*/
    khitemConfirm() {
      this.form.items = this.khitem.selectList;
      this.khitem.open = false
    },
    // 当用户手动勾选数据行的 Checkbox 时触发的事件	    参数 selection, row
    khitemSelect(val,row) {
      let spliceIndex = undefined;
      this.khitem.selectList.forEach((item,index)=>{
        if(item.id === row.id){
          spliceIndex = index
        }
      })
      if(spliceIndex===undefined){
        this.khitem.selectList.push(row)
      }else {
        this.khitem.selectList.splice(spliceIndex,1)
      }
    },
    // 当用户手动勾选全选 Checkbox 时触发的事件	      参数 selection
    khitemSelectAll(val){
      this.$nextTick(()=> {
        if(val.length > 0){
          val.forEach(item=>{
            if(this.khitem.selectList.filter(item1=>{ return item1.id===item.id}).length<1){
              this.khitem.selectList.push(item)
            }
          })
        }else {
          this.khitem.list.forEach(item=>{
            this.khitem.selectList.forEach((item1,index)=>{
              if(item1.id===item.id){
                this.khitem.selectList.splice(index,1)
              }
            })
          })
        }
      })
    },
    // 移除
    selectListDel(row,index){
      this.khitem.selectList.splice(index,1)
      this.khitem.list.forEach(item=>{
        if(row.id===item.id){
          this.$refs.khitemTable.toggleRowSelection(item,false);
        }
      })
    },
    delKhitemList(row,index){
      this.form.items.splice(index,1)
    },
  }
}
</script>
