<template>
  <div class="ele-body">
    <el-card shadow="never" v-loading="loading">
      <inventory-search ref="inventorySearch" @search="reload">
      </inventory-search>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        cache-key="systemRoleTable14"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar="{ row }">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit()"
          >
            新建
          </el-button>
        </template>
        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
          >
            修改
          </el-link>
          <el-link type="primary" class="ele-btn-icon" @click="goDetail(row)">
            详情
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除此角色吗？"
            @confirm="remove(row)"
          >
            <template v-slot:reference>
              <el-link type="danger" :underline="false" icon="el-icon-delete">
                删除
              </el-link>
            </template>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 新建或编辑弹窗 -->
    <AddInventoryDialog
      ref="addInventoryDialogRef"
      :dialogTile="dialogTitle"
      :id="inventoryId"
      @refreshList="resetForm"
      :isBindPlan="isBindPlan"
    />
  </div>
</template>

<script>
  import { deleteStocktakingPlan } from '@/api/ruleManagement/plan';
  import { getInventoryList } from '@/api/ruleManagement/plan';
  import InventorySearch from './components/inventory-search.vue';
  import AddInventoryDialog from './components/addInventoryDialog.vue';
  export default {
    components: {
      InventorySearch,
      AddInventoryDialog
    },
    data() {
      return {
        inventoryId: '',
        // 表格列配置
        columns: [
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'planCode',
            label: '计划单号',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'planName',
            label: '盘点名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'warehouseName',
            label: '盘点仓库',
            align: 'center',
            showOverflowTooltip: true,
            slot: 'enable',
            minWidth: 110
          },
          {
            prop: 'executeGroupName',
            label: '盘点部门',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'categoryLevelName',
            label: '资产分类',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'ruleName',
            label: '规则名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'autoOrder',
            label: '自动派单',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (row) => {
              return { 0: '否', 1: '是' }[row.autoOrder];
            }
          },
          {
            prop: 'enable',
            label: '状态',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (row) => {
              return { 0: '失效', 1: '生效' }[row.enable];
            }
          },
          {
            prop: 'createUserName',
            label: '创建人',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'createTime',
            label: '创建时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (_row, _column, cellValue) => {
              return this.$util.toDateString(cellValue);
            }
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 200,
            align: 'center',
            resizable: false,
            slot: 'action',
            showOverflowTooltip: true
          }
        ],
        // 加载状态
        loading: false,
        dialogTitle: '',
        isBindPlan: false
      };
    },
    methods: {
      // 重置
      resetForm() {
        this.$refs.inventorySearch.reset();
        // this.$refs.configFormRef.resetFields();
        // this.searchForm()
      },
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        return getInventoryList({ pageNum: page, size: limit, ...where });
      },
      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ page: 1, where });
      },
      remove(row) {
        deleteStocktakingPlan([row.id]).then((res) => {
          console.log(res);
          if (res.code == 0) {
            this.$message.success('删除成功');
            this.$refs.inventorySearch.reset();
          }
        });
      },
      openEdit(row) {
        if (row) {
          this.dialogTitle = '修改盘点计划配置';
          this.inventoryId = row.id;
        } else {
          this.dialogTitle = '新增盘点计划配置';
          this.inventoryId = '';
        }
        this.isBindPlan = false;
        this.$refs.addInventoryDialogRef.dialogVisible = true;
        // this.$store.dispatch('planRules/setAddOrUpdate', 'add')
      },
      goDetail(row) {
        this.dialogTitle = '盘点计划配置详情';
        this.inventoryId = row.id;
        this.isBindPlan = true;
        this.$refs.addInventoryDialogRef.dialogVisible = true;
      }
    }
  };
</script>
