<template>
  <div id="stocktakingPlan_index">
    <el-card shadow="never" v-loading="loading">
      <plan-search ref="inventorySearch" @search="reload"> </plan-search>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        height="calc(100vh-300px)"
        :pageSize="20"
        :datasource="datasource"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit()"
          >
            新建临时计划
          </el-button>
        </template>
        <template v-slot:code="{ row }">
          <el-link type="primary" @click="goDetail(row)">
            {{ row.code }}
          </el-link>
        </template>
        <template v-slot:action="{ row }">
          <el-link
            v-if="row.planStatus == 0"
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="assign(row)"
          >
            派单
          </el-link>
          <el-link
            v-if="row.planStatus == 0"
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
          >
            修改
          </el-link>
          <el-link
            v-if="row.planStatus == 0"
            type="danger"
            :underline="false"
            icon="el-icon-delete"
            @click="deleteRow(row)"
          >
            删除
          </el-link>
          <el-link
            v-if="row.planStatus == 1"
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="revocationRow(row)"
          >
            撤回
          </el-link>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 新建或编辑弹窗 -->
    <AddInventoryDialog
      ref="addInventoryDialogRef"
      :dialogTile="dialogTitle"
      :id="inventoryId"
      @refreshList="resetForm"
      :isBindPlan="isBindPlan"
    />
    <!-- 派单 -->
    <AssignDialog
      :visibleDialog.sync="visibleDialog"
      :planId="planId"
      @flushed="reload"
    ></AssignDialog>
  </div>
</template>

<script>
  import PlanSearch from './components/plan-search.vue';
  import AddInventoryDialog from './components/addInventoryDialog.vue';
  import AssignDialog from './components/assignDialog.vue';
  import {
    deleteInventoryPlan,
    getInventoryPlanList,
    revocationInventoryPlan
  } from '@/api/inventory';
  export default {
    components: {
      PlanSearch,
      AddInventoryDialog,
      AssignDialog
    },
    data() {
      return {
        // 表格列配置
        columns: [
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            slot: 'code',
            prop: 'code',
            label: '计划单号',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'name',
            label: '计划名称',
            align: 'center',
            showOverflowTooltip: true,
            slot: 'enable',
            minWidth: 110
          },
          {
            prop: 'warehouseName',
            label: '盘点仓库',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'ruleName',
            label: '计划规则',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'planStatus',
            label: '状态',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (row, column) => {
              return this.statusList.filter(
                (item) => item.value == row.planStatus
              )[0].label;
            }
          },
          {
            prop: 'createUserName',
            label: '创建人',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'createTime',
            label: '生成时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 250,
            align: 'center',
            resizable: false,
            slot: 'action',
            showOverflowTooltip: true
          }
        ],
        // 加载状态
        loading: false,
        dialogTitle: '',
        isBindPlan: false,
        inventoryId: '',
        planId: '',
        // 0未派单;1已派单;2已完成;3已撤回;
        statusList: [
          { label: '未派单', value: 0 },
          { label: '已派单', value: 1 },
          { label: '已完成', value: 2 },
          { label: '已撤回', value: 3 }
        ],
        visibleDialog: false
      };
    },
    methods: {
      assign(row) {
        this.planId = row.id;
        this.visibleDialog = true;
      },
      // 重置
      resetForm() {
        this.$refs.inventorySearch.reset();
      },
      /* 表格数据源 */
      async datasource({ page, limit, where, order }) {
        return await getInventoryPlanList({
          pageNum: page,
          size: limit,
          ...where
        });
      },
      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ page: 1, where });
      },
      openEdit(row) {
        if (row) {
          this.dialogTitle = '修改临时盘点计划';
          this.inventoryId = row.id;
        } else {
          this.dialogTitle = '新增临时盘点计划';
          this.inventoryId = '';
        }
        this.isBindPlan = false;
        this.$refs.addInventoryDialogRef.dialogVisible = true;
      },
      // goDetail(row) {
      //   this.dialogTitle = '临时盘点计划详情';
      //   this.inventoryId = row.id;
      //   this.isBindPlan = true;
      //   this.$refs.addInventoryDialogRef.dialogVisible = true;
      // },
      goDetail(row) {
        this.$router.push({
          path: '/warehouseManagement/stocktaking/plan/details',
          query: {
            id: row.id
          }
        });
      },
      // 删除未派单计划
      deleteRow(row) {
        this.$confirm('是否确定删除该盘点计划?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            await deleteInventoryPlan([row.id]);
            this.$message.success('删除成功!');
            this.$refs.table.reload();
          })
          .catch(() => {});
      },
      // 撤回
      revocationRow(row) {
        this.$confirm('是否确定撤回?', '提示', {
          type: 'warning'
        })
          .then(() => {
            revocationInventoryPlan({ id: row.id }).then((data) => {
              console.log(data);
              this.$message.success('撤回成功!');
              this.reload();
            });
          })
          .catch(() => {});
      }
    }
  };
</script>

<style lang="scss" scoped>
  #stocktakingPlan_index {
    height: calc(100vh - 96px);
    width: 100%;
    padding: 10px;
    box-sizing: border-box;
    // element-ui样式穿透
    :deep(.el-card) {
      height: 100%;
      .el-card__body {
        height: 100%;
        box-sizing: border-box;
        display: flex;
        flex-direction: column;
        .ele-pro-table {
          flex: 1;
          display: flex;
          flex-direction: column;
          .el-table {
            flex: 1;
            // display: flex;
            // flex-direction: column;
            // .el-table__body-wrapper {
            //   flex: 1;
            // }
          }
        }
      }
      .el-form-item {
        margin-bottom: 5px !important;
      }
    }
  }
</style>
