<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      v-model="form"
      v-model:page="page"
      ref="crud"
      @row-del="rowDel"
      @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="addDialog()">新增</el-button>
      </template>
      <template #menu-right> </template>
      <template #menu="{ row }">
        <el-button type="text" @click="detailsDialog(row)">详情</el-button>
        <!-- <el-button
          type="text"
          @click="updateDialog(row)"
        
          >修改</el-button
        > -->
         <!-- <el-button
          type="text"
          @click="review(row)"
         
          >审核</el-button
        > -->
          <el-button
          type="text"
          @click="updateDialog(row)"
          v-if="row.status == '3' || row.status == '4' || row.status == '5'"
          >修改</el-button
        >
        <el-button
          type="text"
          @click="review(row)"
          v-if="row.status == '1' || row.status == '2' || row.status == '0'"
          >审核</el-button
        >
        <el-button type="text" @click="removeLog(row)">删除</el-button>
      </template>
    </avue-crud>

    <!-- 新增 -->
    <addDialog
      v-if="infoShow"
      :showDialog="infoShow"
      @closeDialog="closeDialog"
      :infoTitle="infoTitle"
      :rowItem="rowItem"
      :userListData="userListData"
    ></addDialog>

    <!-- 审核 -->
    <checkDialog
      v-if="checkResultOpen"
      @closeDialog="closeDialog"
      :show-dialog="checkResultOpen"
      :rowItem="rowItem"
    ></checkDialog>

    <!-- 详情 -->
    <detailsDialog
      v-if="detailsOpen"
      :showDialog="detailsOpen"
      @closeDialog="closeDialog"
      :rowItem="rowItem"
      :userListData="userListData"
    ></detailsDialog>
  </basic-container>
</template>

<script>

import { exemptionList, removeItem, userList } from '@/api/exemption/exemption';
import addDialog from './components/addDialog.vue';
import checkDialog from './components/checkDialog.vue';
import detailsDialog from './components/detailsDialog.vue';
export default {
  components: {
    addDialog,
    checkDialog,
    detailsDialog,
  },
  data() {
    return {
      checkResultOpen: false,
      detailsOpen: false,
      infoShow: false,
      infoTitle: '新增',
      form: {},
      selectionList: [],
      query: {},
      loading: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      option: {
        columnSort: true,
        tip: false,
        height: 'auto',
        calcHeight: 32,
        simplePage: false,
        searchShow: true,
        searchMenuSpan: 6,
        searchIcon: true,
        searchIndex: 3,
        tree: false,
        border: true,
        index: true,
        selection: false,
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        editBtnText: '修改',
        labelWidth: 120,
        menuWidth: 180,
        dialogWidth: 900,
        dialogClickModal: false,
        searchEnter: true,
        excelBtn: false,
        filterBtn: true,
        searchShowBtn: false,
        excelBtn: true,
        showOverflowTooltip: true,
        addBtnIcon: ' ',
        viewBtnIcon: ' ',
        delBtnIcon: ' ',
        editBtnIcon: ' ',
        gridBtn: false,
        searchLabelPosition: 'left',
        searchGutter: 24,
        searchSpan: 6,
        menuAlign: 'center',
        gridBtn: false,
        searchMenuPosition: 'right',
        align: 'center',
        column: [
          {
            label: '提出人',
            prop: 'reportUserId',
            sortable: true,
            span: 12,
            formatter: (row, value, label, column) => {
              return this.userMapObj[value] || value || '-';
            },
          },
          {
            label: '岗位',
            prop: 'postName',
            search: true,
            sortable: true,
            span: 12,
          },
          {
            label: '发生时间',
            prop: 'happenDate',
            search: true,
            sortable: true,
            span: 12,
          },
          {
            label: '免责问题描述',
            prop: 'notes',
            search: true,
            sortable: true,
            span: 12,
          },
          {
            label: '原因',
            prop: 'reason',
            search: true,
            sortable: true,
            span: 12,
          },
          {
            label: '措施',
            prop: 'measure',
            search: true,
            sortable: true,
            span: 12,
          },
          {
            label: '免责理由',
            prop: 'raiseHandReason',
            search: true,
            sortable: true,
            span: 12,
          },
          {
            label: '免责金额',
            prop: 'amount',
            search: true,
            sortable: true,
            span: 12,
          },
          // {
          //   label: '固化文件',
          //   prop: 'arr9',
          //   search: true,
          //   sortable: true,
          //   span: 12,
          // },
          {
            label: '状态',
            prop: 'status',
            search: true,
            sortable: true,
            span: 12,
            type: 'select',
            dicData: [
              {
                label: '发起人',
                value: 0,
              },
              {
                label: '工艺员待审核',
                value: 1,
              },
              {
                label: '质量待审核',
                value: 2,
              },
              {
                label: '业务领导待审核 ',
                value: 3,
              },
              {
                label: '审批完成',
                value: 4,
              },
              
            ],
          },
        ],
      },

      data: [],
      rowItem: {},
      userListData: [],
      userMap: {},
    };
  },
  computed: {
    userMapObj() {
      const map = {};
      this.userListData.forEach(user => {
        map[user.value] = user.label;
      });
      return map;
    },
  },
  methods: {
    loadUserList(page, params = {}) {
      userList(page.currentPage, page.pageSize).then(res => {     
        if (res.data.code == 200) {
          const users = res.data.data;
           

          this.userListData = users.map(user => ({
            label: user.realName || user.name,
            value: String(user.id)
          }));
        }
      }).catch(error => {
        console.error('获取用户列表失败', error);
      });
    },
    // 详情
    detailsDialog(row) {
      this.detailsOpen = true;
      this.rowItem = row;
    },
    // 审核
    review(row) {
      this.checkResultOpen = true;
      this.rowItem = row;
    },
    // 新增
    addDialog() {
      this.infoShow = true;
      this.infoTitle = '新增';
      this.rowItem = {};
    },
    // 修改
    updateDialog(row) {
      this.infoShow = true;
      this.infoTitle = '修改';
      this.rowItem = row;
    },
    // 关闭
    closeDialog() {
      this.infoShow = false;
      this.detailsOpen = false;
      this.checkResultOpen = false;
      this.onLoad(this.page, this.query);
    },
    rowSave(row, done, loading) {
      // addPersonAbility(row).then(
      //   () => {
      //     this.onLoad(this.page);
      //     this.$message({
      //       type: 'success',
      //       message: '操作成功!',
      //     });
      //     done();
      //   },
      //   error => {
      //     window.console.log(error);
      //     loading();
      //   }
      // );
    },
    rowUpdate(row, index, done, loading) {
      // updatePersonAbility(row).then(
      //   () => {
      //     this.onLoad(this.page);
      //     this.$message({
      //       type: 'success',
      //       message: '操作成功!',
      //     });
      //     done();
      //   },
      //   error => {
      //     window.console.log(error);
      //     loading();
      //   }
      // );
    },
    removeLog(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          return removeItem(String(row.id));
        })
        .then(() => {
          this.onLoad(this.page, this.query);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
        });
    },

    searchReset() {
      this.query = {};
      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();
    },

    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    handleChange(file, fileList) {
      this.$message({
        type: 'success',
        message: '操作成功!',
      });
    },
 onLoad(page, params = {}) { 
      exemptionList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        console.log(res,'00000001');
        
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
       
      });
    },
  },
  mounted() {
    this.loadUserList({ currentPage: 1, pageSize: 999999 });
  },
};
</script>
d