<!--
@name: 承运商匹配策略
@author: 张泽贤
@time: 2022-10-08
@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.configName" />
              </p-form-model-item>
            </a-col>
            <a-col :span="6">
              <p-form-model-item label="状态">
                <p-select v-model="form.status" :options="options.GENERAL_STATUS" allowClear />
              </p-form-model-item>
            </a-col>
            <a-col :span="12">
              <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>
                  <!-- <p-button type="primary" @click="onSeniorSearch">高级查询</p-button> -->
                </a-space>
              </p-form-model-item>
            </a-col>
          </a-row>
        </p-form-model>
      </p-card>
      <p-card title="承运商匹配策略" style="margin-bottom: 10px">
        <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>
        </a-space>
        <p-table
          ref="masterTable"
          dragle
          bordered
          row-key="id"
          :id="$route.meta.menuId + '_consolidatedBilling'"
          :scroll="{ x: true, y: 300 }"
          :columns="mx_columns"
          :data-source.sync="dataSource"
          :remote-url="listRemoteUrl"
          :remoteData="listParams"
          :rowSelectType="2"
          :selectedRowKeys.sync="selectedRowKeys"
          :sign="true"
        >
          <template slot="operation" slot-scope="text, record">
            <a-space>
              <a @click.stop="onEdit(record, 'view')">查看</a>
              <a @click.stop="onEdit(record, 'edit')">修改</a>
              <p-switch @change="toEnOrDis(record)" :checked="record.status === 10 ? true : false" size="small" />
            </a-space>
          </template>
        </p-table>
      </p-card>
    </p-spin>

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

<script>
import { initDictionary, resultExtendedField } from '@/blocks/utils';

import { listRemoteUrl, updateStatus, Enable, Disable } from './js/api';
import { setMainColumns } from './js/tableConfig.js';
import edit from './modules/edit.vue';

export default {
  components: { edit },
  data() {
    return {
      spinning: false,
      // 空-列表页 view-查看 add-新增 edit-修改
      editType: '',
      editId: '',

      form: {
        strategyName: '',
        status: '',
      },
      options: {
        GENERAL_STATUS: [],
        FEE_RULE: [],
        STRATEGY_CONDITION_TYPE: [],
        DELIVERY_METHOD_TYPE: [],
        SHIPPER_TYPE: [],
        CONSIGNEE_TYPE: [],
        ORDER_TYPE: [],
        ORDER_BUSINESS_TYPE: [],
        TRANSPORT_TYPE: [],
        TEMPERATURE_REQUIREMENT: [],
        LINE_CARRIER_TYPE: [],
        TIME_FREQUENCY_KEY: [],
        TIME_FREQUENCY_VALUE: [],
        SOURCE: [],
      },

      // 主表格
      dataSource: [],
      selectedRowKeys: [],
      listRemoteUrl,
      listParams: {},
      tableExtendedField: [],
      mx_columns: setMainColumns(this, this.tableExtendedField),
    };
  },
  mounted() {
    initDictionary(
      [
        { objectcode: 'GENERAL_STATUS' },
        { objectcode: 'FEE_RULE' },
        { objectcode: 'STRATEGY_CONDITION_TYPE' },
        { objectcode: 'DELIVERY_METHOD_TYPE' },
        { objectcode: 'SHIPPER_TYPE' },
        { objectcode: 'CONSIGNEE_TYPE' },
        { objectcode: 'ORDER_TYPE' },
        { objectcode: 'ORDER_BUSINESS_TYPE' },
        { objectcode: 'TRANSPORT_TYPE' },
        { objectcode: 'TEMPERATURE_REQUIREMENT' },
        { objectcode: 'LINE_CARRIER_TYPE' },
        { objectcode: 'TIME_FREQUENCY_KEY' },
        { objectcode: 'TIME_FREQUENCY_VALUE' },
        { objectcode: 'SOURCE' },
      ],
      true
    ).then((res) => {
      for (const key in res) {
        this.options[key] = res[key];
      }
    });
    // resultExtendedField({ extendedObjectCode: '6' }).then((res) => {
    //   if (res) {
    //     this.tableExtendedField = res.table
    //   }
    // })
  },
  methods: {
    // 查询
    onSearch() {
      this.listParams = { ...this.form };
    },
    // 重置
    onReset() {
      this.form = {
        expenseCode: '',
        expenseName: '',
      };
    },

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

    // 启用、停用
    onEnOrDis(status) {
      this.WhetherToEnable(status === 10 ? Enable : Disable, this.selectedRowKeys, status === 10 ? '启用' : '停用');
    },
    toEnOrDis(record) {
      this.WhetherToEnable(record.status === 10 ? Disable : Enable, [record.id], record.status === 10 ? '停用' : '启用');
    },
    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(() => {
                self.$exception(res);
                resolve();
              });
          });
        },
      });
    },
  },
};
</script>