<template>
  <div class="ele-body">
    <el-card shadow="never" v-loading="loading">
      <materialPlan-search
        @search="reload"
        :statusOpt="statusOpt"
        :activeName="activeName"
        ref="searchRef"
      >
      </materialPlan-search>

      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :initLoad="false"
        :columns="columns"
        :datasource="datasource"
        @columns-change="handleColumnChange"
        :cacheKey="cacheKeyUrl"
           height="calc(100vh - 350px)"
        full-height="calc(100vh - 116px)"
        :page-size="20"
      >
        <template v-slot:toolbar>
          <el-button type="primary" @click="handleEdit('add')">新增</el-button>
        </template>

        <template v-slot:status="{ row }">
          <span :class="{ 'ele-text-danger': row.status == 3 }">
            {{ statusFormatter(row.status) }}
          </span>
        </template>
        <template v-slot:code="{ row }">
          <el-link type="primary" @click="handleEdit('detail', row)">{{
            row.code
          }}</el-link>
        </template>

        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link
            type="primary"
            :underline="false"
            @click="add(row)"
            v-if="row.status != 200 && row.status != 201"
          >
            提交
          </el-link>
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="handleEdit('edit', row)"
            v-if="row.status != 200 && row.status != 201"
          >
            修改计划
          </el-link>
          <el-link
            @click="handleDel(row)"
            type="danger"
            :underline="false"
            icon="el-icon-delete"
            v-if="row.status != 200 && row.status != 201"
          >
            删除
          </el-link>
        </template>
      </ele-pro-table>
    </el-card>
    <produceEditDialog
      ref="produceEditDialogRef"
      @success="reload"
    ></produceEditDialog>
  </div>
</template>

<script>
  import materialPlanSearch from './components/materialPlan-search.vue';

  import produceEditDialog from './components/produce-edit-dialog';
  import { getList, del, addUserPlan } from '@/api/materialPlan/index';
  import tabMixins from '@/mixins/tableColumnsMixin';

  export default {
    components: {
      produceEditDialog,
      materialPlanSearch
    },
    mixins: [tabMixins],

    data() {
      return {
        activeName: 'second',
        columnsVersion: 1,
        cacheKeyUrl: 'c32a9c7d-aps-usePlan-index',
        // 加载状态
        loading: false,

        statusOpt: {
          second: [
            { label: '所有状态', value: null },
            { label: '待排产', value: '1' },
            { label: '待提交', value: '2' },
            { label: '发布失败', value: '3' },
            { label: '已完成', value: '6' },
            { label: '已申请', value: '200' },
            { label: '已入库', value: '201' }
          ]
        }
      };
    },
    computed: {
      // 表格列配置
      columns() {
        let num = this.columnsVersion;

        return [
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            fixed: 'left'
          },
          {
            slot: 'name',
            prop: 'name',
            label: '计划名称',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'code',
            slot: 'code',
            label: '计划编号',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            columnKey: 'status',
            label: '状态',
            align: 'center',
            slot: 'status',
            action: 'status',
            showOverflowTooltip: true
          },
          {
            prop: 'createUserName',
            label: '创建人',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'createTime',
            label: '创建时间',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 250,
            align: 'center',
            resizable: false,
            fixed: 'right',
            slot: 'action'
          }
        ];
      }
    },
    methods: {
      statusFormatter(status) {
        const obj = this.statusOpt[this.activeName].find(
          (i) => i.value == status
        );

        return obj && obj.label;
      },

      /* 表格数据源 */
      datasource({ page, limit, where }) {
        where.type = 2;
        delete where.statusList;
        return getList({
          pageNum: page,
          size: limit,
          ...where
        });
      },
      handleEdit(type, row) {
        this.$refs.produceEditDialogRef.open(type, row);
      },

      handleDel({ id }) {
        this.$confirm('确认删除当前数据！', '提示').then(async () => {
          await del([id]);
          this.$message.success('删除成功！');
          this.reload();
        });
      },

      handleTabChange() {
        this.$refs.searchRef.reset();
      },

      /* 刷新表格 */
      reload(where = {}) {
        if (where.statusList) {
          where.statusList = where.statusList.split(',');
        }
        this.$nextTick(() => {
          this.$refs.table.reload({ page: 1, where });
        });
      },

      //提交
      add(e) {
        addUserPlan({ id: e.id }).then((res) => {
          if (res.code == 200) {
            this.$message.success('提交成功！');
          }
        });
      }
    }
  };
</script>

<style lang="scss" scoped></style>
