<template>
  <basic-container>
    <!-- 绩效模板维护 -->
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      v-model="form"
      v-model:page="page"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template #menu-left>
        <el-button type="primary" @click="handleAdd">新增绩效</el-button>
        <!-- <el-button type="danger" @click="batchDelete">删除</el-button> -->
        <!-- <el-button type="primary" @click="maintenanceContents">绩效内容维护</el-button> -->
      </template>
      <template #menu="scope">
        <el-button type="primary" text @click="editRow(scope.row)">修改</el-button>
        <el-button type="primary" text @click="deleteRow(scope.row)">删除</el-button>
        <el-button type="primary" text @click="taskAssignment(scope.row)">下发</el-button>
      </template>
    </avue-crud>
    <batchAddPerf
      v-if="showDialog"
      :showDialog="showDialog"
      :title="title"
      :checkRow="checkRow"
      :moldAddMore="moldAddMore"
      @closeDialog="closeDialog"
    />
    <!-- <batchAddContent
      v-if="showContent"
      :showContent="showContent"
      :title="title"
      @closeDialog="closeDialog"
    /> -->
  </basic-container>
</template>

<script>
import batchAddPerf from './components/batchAddPerf.vue';
// import batchAddContent from './components/batchAddContent.vue';
import {
  pageBsEfficiencyTemp,
  submitBsEfficiencyTemp,
  detailBsEfficiencyTemp,
  removeBsEfficiencyTemp,
  taskAssignmentBsEfficiencyTemp,
} from '@/api/performanceManagement/templateMaintenance.js';
export default {
  components: {
    batchAddPerf,
    // batchAddContentx
  },
  data() {
    return {
      selectionList: [],
      query: {},
      loading: false,
      data: [],
      form: {},
      showDialog: false,
      moldAddMore: false,
      showContent: false,
      title: '',
      checkRow: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      option: {
        height: 'auto',
        align: 'center',
        calcHeight: 32,
        tip: false,
        simplePage: true,
        searchShow: true,
        searchMenuSpan: 6,
        searchIcon: true,
        searchIndex: 3,
        tree: false,
        border: true,
        index: false,
        selection: true,
        viewBtn: false,
        delBtn: false,
        addBtn: false,
        editBtn: false,
        editBtnText: '修改',
        addBtnIcon: ' ',
        viewBtnIcon: ' ',
        delBtnIcon: ' ',
        editBtnIcon: ' ',
        viewBtnText: '详情',
        labelWidth: 120,
        searchLabelWidth: 120,
        menu: true,
        menuWidth: 150,
        dialogWidth: 1200,
        dialogClickModal: false,
        searchEnter: true,
        excelBtn: false,
        filterBtn: true,
        searchShowBtn: false,
        columnSort: true,
        excelBtn: true,
        columnSort: true,
        showOverflowTooltip: true,
        searchLabelPosition: 'left',
        searchLabelPosition: 'left',
        searchGutter: 24,
        searchSpan: 6,
        menuAlign: 'center',
        gridBtn: false,
        searchMenuPosition: 'right',
        column: [
          // {
          //   label: "模板类型",
          //   prop: "templateType",
          //   type: "select",
          //   search: true,
          //   sortable: true,
          //   overHidden: true,
          //   dicData: [
          //     { label: "通用", value: 1 },
          //     { label: "非通用", value: 2 },
          //   ],
          //   rules: [
          //     {
          //       required: true,
          //       message: "请输入模板类型",
          //       trigger: "blur",
          //     },
          //   ],
          //   control: (val, form) => {
          //     if (val == 1) {
          //       return {
          //         applicationDept: {
          //           rules: [
          //             {
          //               required: true,
          //               message: "请输入模板应用部门",
          //               trigger: "blur",
          //             },
          //           ],
          //         },
          //       };
          //     } else {
          //       return {
          //         applicationDept: {
          //           rules: [],
          //         },
          //       };
          //     }
          //   },
          // },
          // {
          //   label: "模板应用部门",
          //   prop: "applicationDept",
          //   type: "select",
          //   search: true,
          //   sortable: true,
          //   overHidden: true,
          //   dicData: [
          //     { label: "质量", value: 1 },
          //     { label: "工艺", value: 2 },
          //     { label: "辅助人员", value: 3 },
          //     { label: "生产线", value: 4 },
          //   ],
          //   // rules: [
          //   //     {
          //   //         required: true,
          //   //         message: '请输入模板应用部门',
          //   //         trigger: 'blur',
          //   //     },
          //   // ],
          // },
          {
            label: '绩效任务名称',
            prop: 'taskName',
            search: false,
            sortable: true,
            overHidden: true,
            rules: [
              {
                required: true,
                message: '请输入绩效任务名称',
                trigger: 'blur',
              },
            ],
          },
          // {
          //   label: "模板内容",
          //   prop: "templateContent",
          //   search: false,
          //   width: 500,
          //   sortable: true,
          //   overHidden: true,
          //   rules: [
          //     {
          //       required: true,
          //       message: "请输入模板内容",
          //       trigger: "blur",
          //     },
          //   ],
          // },
          {
            label: '绩效填报人',
            prop: 'reportUserName',
            type: 'select',
            search: false,
            sortable: true,
            overHidden: true,
            rules: [
              {
                required: true,
                message: '请输入绩效填报人',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '维护人',
            prop: 'createUser',
            search: false,
            sortable: true,
            overHidden: true,
          },
          {
            label: '维护时间',
            prop: 'createTime',
            search: false,
            sortable: true,
            overHidden: true,
          },
        ],
      },
    };
  },
  mounted() {},
  methods: {
    closeDialog(val) {
      this.showDialog = false;
      this.moldAddMore = false;
      // this.showContent = false
      if (val) {
        this.onLoad();
      }
    },
    handleAdd() {
      this.title = '新增';
      this.moldAddMore = true;
      this.showDialog = true;
    },
    editRow(row) {
      this.title = '修改';
      row.reportUser = row.reportUser + '';
      this.checkRow = { ...row };
      this.moldAddMore = false;
      this.showDialog = true;
    },
    // 批量删除
    batchDelete() {
      if (this.selectionList.length == 0) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      const count = this.selectionList.length;
      this.$confirm(`确定要删除选中的 ${count} 条绩效模板吗？此操作不可恢复！`, '删除确认', {
        confirmButtonText: '确定删除',
        cancelButtonText: '取消',
        type: 'warning',
        confirmButtonClass: 'el-button--danger',
      })
        .then(() => {
          removeBsEfficiencyTemp({
            ids: this.selectionList.map(item => item.id).join(','),
          }).then(res => {
            if (res.data.code == 200) {
              this.$message.success('删除成功');
              this.selectionList = [];
              this.onLoad();
            }
          });
        })
        .catch(() => {
          // 用户点击取消
        });
    },
    deleteRow(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          // 单条直接转字符串传
          removeBsEfficiencyTemp({
            ids: String(row.id),
          })
            .then(res => {
              if (res.data.code == 200) {
                this.$message.success('删除成功');
                this.onLoad();
              }
            })
            .catch(() => {
              this.$message.error('删除失败，请重试');
            });
        })
        .catch(() => {});
    },
    taskAssignment(row) {
      this.$confirm('确定将选择数据下发?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          // 单条直接转字符串传
          taskAssignmentBsEfficiencyTemp({
            id: row.id,
          })
            .then(res => {
              if (res.data.code == 200) {
                this.$message.success(res.data.msg);
                this.onLoad();
              }
            })
            .catch(() => {});
        })
        .catch(() => {});
    },
    // maintenanceContents() {
    //   this.title = '模板内容维护';
    //   this.showContent = true;
    // },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    searchReset() {
      this.query = {};
      this.treeDeptId = '';
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    onLoad() {
      this.loading = true;
      pageBsEfficiencyTemp({
        current: this.page.currentPage,
        size: this.page.pageSize,
        ...this.query,
      }).then(res => {
        this.data = res.data.data.records;
        this.page.total = res.data.data.total;
        this.loading = false;
      });
    },
  },
};
</script>