<!--
@name: 商品品类管理
@author: 张泽贤
@time: 2022-09-15
@doc: PROLOG_UPCloudSolutionForTMS__v1.2.1_基础数据_1.2.1.doc
-->
<template>
  <div class="pages">
    <p-spin :spinning="spinning" v-if="!editType">
      <p-card style="margin-bottom: 10px">
        <p-form-model ref="formTarget" :model="form" :label-col="{ span: 8 }" :wrapper-col="{ span: 16 }" class="plg-grid-from">
          <a-row>
            <a-col :span="6">
              <p-form-model-item label="商品品类编码">
                <p-input v-model="form.itemCategoryCode" />
              </p-form-model-item>
            </a-col>
            <a-col :span="6">
              <p-form-model-item label="商品品类名称">
                <p-input v-model="form.itemCategoryName" />
              </p-form-model-item>
            </a-col>
            <a-col :span="6">
              <p-form-model-item label="所属货主">
                <p-input v-model="form.cargoOwnerName" />
              </p-form-model-item>
            </a-col>
            <a-col :span="6">
              <p-form-model-item :wrapper-col="{ span: 24 }" style="text-align: right">
                <a-space>
                  <p-button type="primary" @clickSync="onSearch" icon="search">查询</p-button>
                  <p-button @click="onReset">重置</p-button>
                </a-space>
              </p-form-model-item>
            </a-col>
          </a-row>
        </p-form-model>
      </p-card>
      <p-card title="商品品类信息">
        <a-space slot="extra">
          <p-button type="primary" @click="onEdit({}, 'add')">新增</p-button>
          <p-button type="primary" @click="onEnOrDis(10)" :disabled="!selectedRowKeys.length">启用</p-button>
          <p-button type="danger" @click="onEnOrDis(0)" :disabled="!selectedRowKeys.length">停用</p-button>
          <p-button type="danger" @click="onDel" :disabled="!selectedRowKeys.length">删除</p-button>
          <import-Button :importUrl="importUrl" :tmpUrl="downUrl" fileName="商品品类模板" />
        </a-space>
        <p-table
          ref="masterTable"
          dragle
          bordered
          :extendedObjectCode="'1'"
          row-key="itemCategoryId"
          :id="$route.meta.menuId + '_itemCategory'"
          :scroll="{ x: true, y: 300 }"
          :columns="mx_columns"
          :data-source.sync="dataSource"
          :remote-url="listRemoteUrl"
          :remoteData="listParams"
          :rowSelectType="2"
          :selectedRowKeys.sync="selectedRowKeys"
        >
          <template slot="operation" slot-scope="text, record">
            <a-space>
              <a @click.stop="onEdit(record, 'view')">查看</a>
              <a @click.stop="onEdit(record, 'copy')">复制</a>
              <a @click.stop="onEdit(record, 'edit')">修改</a>
              <p-switch @change="toEnOrDis(record)" :checked="record.status === 10 ? true : false" size="small" />
              <a @click.stop="toDel(record)">删除</a>
            </a-space>
          </template>
        </p-table>
      </p-card>
    </p-spin>

    <!-- 编辑 -->
    <edit ref="edit" v-if="editType" :type="editType" :editData="editData" :options="options" :mextendedField_list="mextendedField_list" @back="editType = ''"></edit>
  </div>
</template>

<script>
import { initDictionary, resultExtendedField } from '@/blocks/utils';
import importButton from '@/blocks/components/importButton/index.importButton.vue';

import { setMainColumns } from './js/tableConfig.js';
import { listRemoteUrl, importUrl, downUrl, updateStatus, Delete } from './js/api.js';
import edit from './modules/edit.vue';

export default {
  components: { edit, importButton },
  data() {
    return {
      spinning: false,
      editType: '',
      editData: {},
      importUrl,
      downUrl,

      form: {
        itemCategoryCode: '',
        itemCategoryName: '',
        cargoOwnerName: '',
      },
      options: {
        ITEM_CATEGORY_LEVEL: [],
      },

      // 主表格
      dataSource: [],
      selectedRowKeys: [],
      listRemoteUrl,
      listParams: {},
      tableExtendedField: [],
      mextendedField_list: [],
      mx_columns: setMainColumns(this, this.tableExtendedField),

      importUrl,
      downUrl,
    };
  },
  mounted() {
    initDictionary([{ objectcode: 'ITEM_CATEGORY_LEVEL' }], true).then((res) => {
      for (const key in res) {
        this.options[key] = res[key];
      }
    });
    resultExtendedField({ extendedObjectCode: '1' }).then((res) => {
      if (res) {
        this.mextendedField_list = res.list;
        this.tableExtendedField = res.table;
        this.mx_columns = setMainColumns(this, this.tableExtendedField);
      }
    });
  },
  methods: {
    // 查询
    onSearch() {
      this.listParams = { ...this.form };
    },
    // 重置
    onReset() {
      this.form = {
        itemCategoryCode: '',
        itemCategoryName: '',
        cargoOwnerName: '',
      };
    },

    // 新增、修改、复制、查看
    onEdit(record = { itemCategoryId: '' }, type) {
      this.editData = { ...record };
      this.editType = type;
    },

    // 启用、停用
    onEnOrDis(status) {
      let list = this.selectedRowKeys.map((item) => {
        return {
          itemCategoryId: item,
        };
      });
      this.WhetherToEnable(updateStatus, { itemCategoryIds: list, status }, status === 10 ? '启用' : '停用');
    },
    toEnOrDis(record) {
      let request = {
        itemCategoryIds: [{ itemCategoryId: record.itemCategoryId }],
        status: record.status === 10 ? 0 : 10,
      };
      this.WhetherToEnable(updateStatus, request, record.status === 10 ? '停用' : '启用');
    },
    // 删除
    onDel() {
      let list = this.selectedRowKeys.map((itemCategoryId) => {
        return { itemCategoryId };
      });
      this.WhetherToEnable(Delete, list, '删除');
    },
    toDel(record) {
      this.WhetherToEnable(Delete, [{ itemCategoryId: record.itemCategoryId }], '删除');
    },
    WhetherToEnable(method, idList, text) {
      let self = this;
      this.$confirm({
        title: '是否' + text + '?',
        onOk() {
          return new Promise((resolve, reject) => {
            method(idList)
              .then((res) => {
                if (res.success) {
                  self.$message.success(res.message);
                  self.onSearch();
                } else {
                  self.$exception(res);
                }
                resolve();
              })
              .catch((res) => {
                self.$exception(res);
                resolve();
              });
          });
        },
      });
    },
  },
};
</script>