<template>
  <div>
    <!-- 数据表格 -->
    <ele-pro-table
      ref="table"
      :initLoad="false"
      :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>
        <item-search @search="reload"> </item-search>
      </template>
      <!-- 操作列 -->
      <template v-slot:action="{ row }">
        <el-link
          type="primary"
          :underline="false"
          icon="el-icon-edit"
          @click="viewEdit(row)"
        >
          详情
        </el-link>
        <el-link
          type="primary"
          :underline="false"
          icon="el-icon-edit"
          @click="openEdit(row)"
        >
          编辑
        </el-link>
        <el-link
          type="primary"
          :underline="false"
          icon="el-icon-unlock"
          @click="openClone(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>
  </div>
</template>

<script>
  import ItemSearch from './item-search.vue';
  // import {
  //   getUserPage,
  //   removePersonnel,
  //   unbindLoginName
  // } from '@/api/system/organization';
  import { getList, removeItem } from '@/api/classifyManage/itemInformation';

  export default {
    components: { ItemSearch },
    props: {
      // 机构id
      organizationId: [Number, String],
      // 全部机构
      organizationList: Array,
      current: {
        type: Object,
        default: () => ({})
      }
    },
    data () {
      return {};
    },
    computed: {
      // 表格列配置
      columns () {
        const privateColumn = [];

        if (!['8', '1', '2', '5', '6', '7'].includes(this.current?.type)) {
          privateColumn.push({
            prop: 'brandNum',
            label: '牌号',
            showOverflowTooltip: true
          });
        }
        if (!['3'].includes(this.current?.type)) {
          privateColumn.push({
            prop: 'modelType',
            label: '型号',
            showOverflowTooltip: true
          });
        }
        if (!['3', '4'].includes(this.current?.type)) {
          privateColumn.push({
            prop: 'specification',
            label: '规格',
            showOverflowTooltip: true
          });
        }

        return [
          {
            columnKey: 'index',
            type: 'index',
            width: 45,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'code',
            label: '物品编码',
            showOverflowTooltip: true
          },
          {
            prop: 'name',
            label: '物品名称',
            showOverflowTooltip: true
          },
          {
            prop: 'classificationCode',
            label: '物品类型',
            showOverflowTooltip: true
          },
          ...privateColumn,
          {
            prop: 'measuringUnit',
            label: '计量单位',
            sortable: 'custom',
            showOverflowTooltip: true
          },
          {
            prop: 'packingUnit',
            label: '包装单位',
            align: 'center'
          },
          {
            prop: 'categoryLevelPath',
            label: '分类',
            showOverflowTooltip: true
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 250,
            align: 'left',
            resizable: false,
            slot: 'action'
          }
        ];
      }
    },
    methods: {
      /* 表格数据源 */
      datasource ({ page, limit, where }) {
        console.log(this.where);
        return getList({
          ...where,
          pageNum: page,
          size: limit
        });
      },
      /* 刷新表格 */
      reload (where) {
        this.$nextTick(() => {
          this.$refs.table.reload({
            pageNum: 1,
            where: {
              ...where,
              categoryLevelId: this.current?.data?.id || this.current?.id
            }
          });
        });
      },
      /* 显示详情 */
      viewEdit (row) {
        this.$router.push({
          path: '/classifyManage/itemInformation/details',
          query: {
            id: row.id
          }
        });
      },
      /* 显示编辑 */
      openEdit (row) {
        this.$router.push({
          path: '/classifyManage/itemInformation/add',
          query: {
            pageTitle: '编辑物品',
            id: row.id
          }
        });
      },
      /* 显示编辑 */
      openClone (row) {
        this.$router.push({
          path: '/classifyManage/itemInformation/add',
          query: {
            pageTitle: '克隆物品',
            id: row.id
          }
        });
      },
      /* 删除 */
      remove (row) {
        const loading = this.$loading({ lock: true });
        removeItem(id).then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        });
      },
      /* 更改状态 */
      editStatus (row) {
        const loading = this.$loading({ lock: true });
        updateUserStatus(row.userId, row.status)
          .then((msg) => {
            loading.close();
            this.$message.success(msg);
          })
          .catch((e) => {
            loading.close();
            row.status = !row.status ? 1 : 0;
            this.$message.error(e.message);
          });
      }
    },
    watch: {
      // 监听机构id变化
      current: {
        handler () {
          console.log(this.current, 'current');
          this.reload();
        }
      }
    }
  };
</script>

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