<template>
  <div>
    <user-search @search="reload" ref="searchRef">
    </user-search>
    <!-- 数据表格 -->
    <ele-pro-table ref="table" :columns="columns" :datasource="datasource" height="calc(100vh - 265px)"
      full-height="calc(100vh - 116px)" tool-class="ele-toolbar-form" cache-key="systemOrgUserTable">
      <!-- 表头工具栏 -->
      <template v-slot:toolbar>
        <el-button size="small" type="primary" icon="el-icon-plus" class="ele-btn-icon" @click="openAdd()">
          新增
        </el-button>
      </template>
      <!-- 编码列 -->

      <template v-slot:name="{ row }">
        {{ row.qualityStandard.name }}
      </template>


      <template v-slot:code="{ row }">
        {{ row.qualityStandard.code }}
      </template>



      <template v-slot:status="{ row }">
        {{ row.status == 1 ? '启用' : '停用' }}
      </template>


      <template v-slot:mode="{ row }">
        {{getDictValue('质检方式', row.mode)}}

      </template>



      <template v-slot:version="{ row }">
        {{ row.version }}
      </template>



      <template v-slot:action="{ row }">
        <el-link type="primary" :underline="false" icon="el-icon-edit" @click="openEdit(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>

    <Add @chooseProcess="chooseProcess" ref="addRef" />

    <edit ref="edit" @done="done"></edit>

  </div>
</template>

<script>
import userSearch from './user-search.vue';
import { getList, removeItem, saveBatch } from '@/api/inspectionClassify/index';
import dictMixins from '@/mixins/dictMixins';
import Add from './add.vue';
import Edit from './edit.vue'

export default {
  mixins: [dictMixins],
  components: { userSearch, Add, Edit },
  props: {
    // 类别id
    categoryId: [Number, String],
    rootId: [Number, String],
  },
  data() {
    return {
      // 当前编辑数据
      current: null,



      // 表格列配置
      columns: [
        {
          columnKey: 'index',
          type: 'index',
          label: '序号',
          width: 55,
          align: 'center',
        },

        {
          prop: 'name',
          label: '标准名称',
          showOverflowTooltip: true,
          minWidth: 110,
          slot: 'name',
        },

        {
          prop: 'code',
          label: '标准编码',
          showOverflowTooltip: true,
          minWidth: 110,
          slot: 'code',
        },


        {
          prop: 'status',
          label: '状态',
          align: 'center',
          minWidth: 110,
          slot: 'status',
        },



        {
          prop: 'mode',
          label: '类型',
          align: 'center',
          minWidth: 110,
          slot: 'mode',
        },

        {
          prop: 'version',
          label: '版本号',
          align: 'center',
          minWidth: 110,
          slot: 'version',
        },

        {
          label: '操作',
          prop: 'action',
          slot: 'action',
          action: 'action'
        }




      ]
    };
  },
  created() {
    this.requestDict('质检方式');
  },
  methods: {
    /* 表格数据源 */
    datasource({ page, limit, where }) {
      return getList({
        ...where,
        pageNum: page,
        size: limit,
        categoryLevelId: this.categoryId,
        rootCategoryLevelId: this.rootId
      });
    },
    /* 刷新表格 */
    reload(where) {

      this.$refs.table.reload({ pageNum: 1, where: where ,  categoryLevelId: this.categoryId,  rootCategoryLevelId: this.rootId});
    },

    /* 打开编辑弹窗 */
    openAdd() {
      this.$refs.addRef.open(this.categoryId);
    },

    openEdit(row) {
      this.$refs.edit.open(row);
    },

    chooseProcess(data) {
      saveBatch(data).then(res => {
        console.log(res)
        if (res.code == 0) {
          this.$message.success(res.message)
          this.reload()
        }
      })

    },


    /* 删除 */
    remove(row) {
      const loading = this.$loading({ lock: true });
      removeItem([row.id])
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
        });
    },
    done() {
      this.$refs.searchRef.search();
    }



  },
  watch: {
    // 监听类别id变化
    categoryId() {
      this.reload();
    }
  }
};
</script>
