<template>
  <div class="ele-body">
    <el-card shadow="never">
      <seek-page :seekList="seekList" @search="search"></seek-page>
      <el-tabs
        v-model="activeName"
        @tab-click="handleClick"
        type="card"
        style="margin-top: 5px"
      >
        <el-tab-pane label="全部样品" name="1"></el-tab-pane>
        <el-tab-pane label="整样样品" name="2"></el-tab-pane>
        <el-tab-pane label="小样样品" name="3"></el-tab-pane>
      </el-tabs>
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :pageSize="20"
      >
        <!-- 表头工具栏 -->

        <template v-slot:toolbar>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit('add')"
          >
            新增
          </el-button>
        </template>

        <template v-slot:sampleCode="{ row }">
          <el-link
            type="primary"
            :underline="false"
            @click="goDetail(row.id, 'detail')"
            >{{ row.sampleCode }}</el-link
          >
        </template>

        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit('edit', row)"
          >
            修改
          </el-link>
          <el-link
            type="primary"
            :underline="false"
            @click="goDetail(row.id)"
            v-if="row.status !== 2"
          >
            处置
          </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>
    <!-- 新增的dialog -->
    <sample ref="sampleRef" @done="reload"></sample>
  </div>
</template>
<script>
  import sample from './components/addOrEditSample.vue';
  import { getList, removeItem } from '@/api/samplemanagement';
  import dictMixins from '@/mixins/dictMixins';

  export default {
    mixins: [dictMixins],
    components: {
      sample
    },
    data() {
      return {
        columns: [
          {
            type: 'index',
            columnKey: 'index',
            align: 'center',
            label: '序号',
            width: 55,
            showOverflowTooltip: true
          },
          {
            prop: 'sampleCode',
            label: '编码',
            slot: 'sampleCode',
            align: 'center',
            width: 160,
            showOverflowTooltip: true
          },
          {
            prop: 'sourceCode',
            label: '来源编码',
            align: 'center',
            width: 160,
            showOverflowTooltip: true
          },
          {
            prop: 'categoryCode',
            label: '物品编码',
            align: 'center',
            width: 160,
            showOverflowTooltip: true
          },
          {
            prop: 'categoryName',
            label: '物品名称',
            align: 'center',
            width: 160,
            showOverflowTooltip: true
          },
          {
            prop: 'batchNo',
            label: '批次号',
            align: 'center',
            width: 100,
            showOverflowTooltip: true
          },
          {
            prop: 'specification',
            label: '规格',
            align: 'center',
            width: 100,
            showOverflowTooltip: true
          },
          {
            prop: 'brandNum',
            label: '牌号',
            align: 'center',
            width: 100,
            showOverflowTooltip: true
          },
          {
            prop: 'modelType',
            label: '型号',
            align: 'center',
            width: 160,
            showOverflowTooltip: true
          },
          {
            prop: 'quantity',
            label: '样品数量',
            align: 'center',
            width: 100,
            showOverflowTooltip: true
          },
          // {
          //   prop: 'unit',
          //   label: '计量单位',
          //   align: 'center',
          //   width: 100,
          //   showOverflowTooltip: true
          // },

          {
            prop: 'disposeTime',
            label: '处置时间',
            align: 'center',
            width: 180,
            showOverflowTooltip: true
          },
          {
            prop: 'status',
            label: '状态',
            align: 'center',
            width: 80,
            formatter: (row, column, cellValue) => {
              console.log(this.getDictName('处置状态', cellValue));
              
              return this.getDictName('处置状态', cellValue);
            }
          },
          {
            columnKey: 'action',
            label: '操作',
            align: 'center',
            width: 180,
            resizable: false,
            slot: 'action',
            fixed: 'right'
          }
        ],
        activeName: '1'
      };
    },
    created() {
      this.requestDict('处置状态');
    },

    computed: {
      seekList() {
        return [
          {
            label: '编码:',
            value: 'sampleCode',
            type: 'input',
            placeholder: ''
          },
          {
            label: '来源编码:',
            value: 'sourceCode',
            type: 'input',
            placeholder: ''
          },
          {
            label: '物品编码:',
            value: 'categoryCode',
            type: 'input',
            placeholder: ''
          },
          {
            label: '物品名称:',
            value: 'categoryName',
            type: 'input',
            placeholder: ''
          },
          {
            label: '状态:',
            value: 'status',
            type: 'select',
            placeholder: '',
            planList: [
              { value: 0, label: '待处置' },
              { value: 1, label: '部分处置' },
              { value: 2, label: '处置完成' }
            ]
          }
        ];
      }
    },

    methods: {
      datasource({ page, where, limit }) {
        return getList({
          ...where,
          pageNum: page,
          size: limit
        });
      },

      search(where) {
        this.reload(where);
      },

      reload(where = {}) {
        this.$nextTick(() => {
          where.conditionType =
            this.activeName == '1' ? '' : this.activeName == '2' ? '1' : '2';
          this.$refs.table.reload({ page: 1, where });
        });
      },
      goDetail(id, type) {
        this.$router.push({
          path: '/sample/samplemanagement/detailList',
          query: {
            id,
            type
          }
        });
      },
      handleClick(tab) {
        this.activeName = tab.name;
        this.reload();
      },
      openEdit(type, row) {
        this.$refs.sampleRef.open(type, row);
      },

      remove(item) {
        removeItem([item.id])
          .then((message) => {
            this.$message.success(message);
            this.reload();
          })
          .catch((e) => {});
      }
    }
  };
</script>

<style scoped></style>
