<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"
        cache-key="sample"
        :height="tableHeight"
        @fullscreen-change="fullscreenChange"
      >
        <!-- 表头工具栏 -->

        <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:status="{ row }">
          <span>{{ getStatusLabel(row) }}</span>
        </template>

        <template v-slot:quantity="{ row }">
          <span>{{ row.quantity }}{{ row.measureUnit }}</span>
        </template>

        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit('edit', row)"
            v-if="row.status !== 2"
          >
            修改
          </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)"
            v-if="row.status !== 2"
          >
            <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/sample';
  import dictMixins from '@/mixins/dictMixins';
  import { getByCode } from '@/api/system/dictionary-data';

  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',
            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',
            slot: 'quantity',
            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: '状态',
            slot: 'status',
            align: 'center',
            width: 80
          },
          {
            columnKey: 'action',
            label: '操作',
            align: 'center',
            width: 180,
            resizable: false,
            slot: 'action',
            fixed: 'right'
          }
        ],
        activeName: '1',
        disposeStatusList: [],
        tableHeight: 'calc(100vh - 370px)'
      };
    },
    created() {
      // this.requestDict('处置状态');
      this.getDisposeStatus('dispose_status');
    },

    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
        });
      },

      fullscreenChange(fullscreen) {
        if (fullscreen) {
          this.tableHeight = 'calc(100vh - 120px)';
        } else {
          this.tableHeight = 'calc(100vh - 370px)';
        }
      },

      async getDisposeStatus(code) {
        let res = await getByCode(code);
        if (res?.code == 0) {
          let list = res.data.map((item) => {
            let key = Object.keys(item)[0];
            return { value: key, label: item[key] };
          });
          this.disposeStatusList = list;
        }
      },

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

      getStatusLabel(row) {
        const res = this.disposeStatusList.filter(
          (item) => item.value == row.status
        );

        return res && res.length != 0 ? res[0].label : '';
      },

      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/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>
