<!--
@name: 运费价格、附加费价格表格
@author: 张泽贤
@time: 2022-08-29
@doc:
-->
<template>
  <p-card title="">
    <a-space slot="extra">
      <p-button type="primary" @click="onEdit({}, priceType + 'Add')" :disabled="!contractNo">新增</p-button>
      <p-button type="primary" @click="onEnOrDis(1)" :disabled="!selectedRowKeys.length">启用</p-button>
      <p-button type="danger" @click="onEnOrDis(0)" :disabled="!selectedRowKeys.length">停用</p-button>
      <import-Button :importUrl="importUrl" :tmpUrl="downUrl" :fileName="(priceType === 'freight' ? '运费价格' : '附加费价格') + '模板'" />
      <export-button
        :ids="selectedRowKeys"
        :pages="mainPages"
        :getSearchValue="() => listParams"
        :downloadFileName="priceType === 'freight' ? '运费价格' : '附加费价格'"
        :allUrl="allUrl"
        :currentUrl="currentUrl"
        :selectUrl="selectUrl"
        >导出</export-button
      >
    </a-space>
    <t-table
      ref="priceTable"
      dragle
      bordered
      row-key="id"
      :id="$route.meta.menuId + '_priceTable'"
      :scroll="{ x: true }"
      :vScrollX="{enabled:false}"
      :vScrollY="{enabled:true,rows:6}"
      :columns="mx_columns"
      :data-source.sync="dataSource"
      :remote-url="listRemoteUrl"
      :remoteData="listParams"
      :rowSelectType="2"
      @selectChange="(selectKey, selectData)=>selectedRowKeys=selectKey"
      :sign="true"
      :autoLoad="false"
      :doneBefore="doneBefore"
    >
      <template slot="operation" slot-scope="text, record">
        <a-space>
          <a @click.stop="onEdit(record, priceType + 'View')">查看</a>
          <a @click.stop="onEdit(record, priceType + 'Edit')">修改</a>
          <p-switch @change="toEnOrDis(record)" :checked="record.status === 1 ? true : false" size="small" />
          <a @click.stop="toDel(record)">删除</a>
        </a-space>
      </template>
    </t-table>
  </p-card>
</template>

<script>
import importButton from '@/blocks/components/importButton/index.importButton.vue';
import exportButton from '@/blocks/components/export-modal/modal.index.vue';

import { priceListRemoteUrl, priceUpdateStatus, priceDelete, importUrl, downUrl, allUrl, currentUrl, selectUrl } from '../js/api.js';
import { setPriceColumns } from '../js/tableConfig.js';

export default {
  components: { importButton, exportButton },
  props: {
    priceType: {
      type: String,
      default: '',
    },
    contractNo: {
      type: String,
      default: '',
    },
    isShow: {
      type: Boolean,
      default: false,
    },
  },
  watch: {
    contractNo(val) {
      if (val && this.isShow) {
        this.listParams = {
          contractNo: val,
          isSurcharge: this.priceType === 'freight' ? 0 : 1,
        };
      } else {
        this.dataSource = [];
      }
    },
  },
  data() {
    return {
      // 主表格
      dataSource: [],
      selectedRowKeys: [],
      listRemoteUrl: priceListRemoteUrl,
      listParams: {},
      mx_columns: setPriceColumns(this),
      mainPages: { pageNum: 1, pageSize: 15 },

      importUrl,
      downUrl,
      allUrl,
      currentUrl,
      selectUrl,
    };
  },
  methods: {
    // 新增、修改、复制、查看
    onEdit(record = { id: '' }, type) {
      // this.editId = record.id
      // this.editType = type
      this.$emit('priceOperation', { priceId: record.id, editType: type });
    },

    // 启用、停用
    onEnOrDis(status) {
      let list = this.selectedRowKeys.map((item) => {
        return {
          id: item,
          status,
        };
      });
      this.WhetherToEnable(priceUpdateStatus, list, status === 1 ? '启用' : '停用');
    },
    toEnOrDis(record) {
      let reuest = [
        {
          id: record.id,
          status: record.status === 1 ? 0 : 1,
        },
      ];
      this.WhetherToEnable(priceUpdateStatus, reuest, record.status === 1 ? '停用' : '启用');
    },
    // 删除
    onDel() {
      let list = this.selectedRowKeys.map((item) => {
        return { id: item };
      });
      this.WhetherToEnable(priceDelete, list, '删除');
    },
    toDel(record) {
      this.WhetherToEnable(priceDelete, [{ id: record.id }], '删除');
    },
    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()
                  self.$refs.priceTable.reload();
                } else {
                  self.$exception(res);
                }
                resolve();
              })
              .catch((res) => {
                self.$exception(res);
                resolve();
              });
          });
        },
      });
    },
    doneBefore(res) {
      const { pageNum, pageSize } = res.data;
      this.mainPages = { pageNum, pageSize };
    },

    reload(args) {
      this.dataSource = [];
      this.$refs.priceTable.reload({
        data: {
          contractNo: this.contractNo,
          isSurcharge: this.priceType === 'freight' ? 0 : 1,
        },
      });
    },
  },
};
</script>