<template>
  <div>
    <p-form-model ref="ruleForm" class="plg-grid-from" :model="form" :label-col="{ span: 8 }" :wrapper-col="{ span: 16 }" :rules="rules">
      <a-divider orientation="left">基本信息</a-divider>
      <a-row>
        <a-col :span="6">
          <p-form-model-item label="价格编号" prop="priceNo">
            <div v-if="!isFocus">{{ form.priceNo }}</div>
            <p-input v-model="form.priceNo" :disabled="type.includes('Edit')" v-else />
          </p-form-model-item>
        </a-col>
        <a-col :span="6">
          <p-form-model-item label="价格名称" prop="priceName">
            <div v-if="!isFocus">{{ form.priceName }}</div>
            <p-input v-model="form.priceName" :disabled="type.includes('Edit')" v-else />
          </p-form-model-item>
        </a-col>
        <a-col :span="6">
          <p-form-model-item label="始发地" prop="shipperProvinceName">
            <div v-if="!isFocus">{{ (form.shipperProvinceName || '') + (form.shipperCityName || '') + (form.shipperDistrictName || '') }}</div>
            <p-cascader-zone :value="shipperZoneValue" @change="shipperZoneChange" :level="3" v-else />
          </p-form-model-item>
        </a-col>
        <a-col :span="6">
          <p-form-model-item label="目的地" prop="consigneeProvinceName">
            <div v-if="!isFocus">{{ (form.consigneeProvinceName || '') + (form.consigneeCityName || '') + (form.consigneeDistrictName || '') }}</div>
            <p-cascader-zone :value="consigneeZoneValue" @change="consigneeZoneChange" :level="3" v-else />
          </p-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="6">
          <p-form-model-item label="时效(时)" prop="aging">
            <div v-if="!isFocus">{{ form.aging }}</div>
            <p-input-number v-model="form.aging" v-else />
          </p-form-model-item>
        </a-col>
        <a-col :span="6">
          <p-form-model-item label="里程(KM)" prop="mileage">
            <div v-if="!isFocus">{{ form.mileage }}</div>
            <p-input-number v-model="form.mileage" v-else />
          </p-form-model-item>
        </a-col>
        <a-col :span="6">
          <p-form-model-item label="费用" prop="expenseName">
            <div v-if="!isFocus">{{ form.expenseName }}</div>
            <p-search-modal v-model="form.expenseName" v-bind="expenseDriverAttr" @change="(...args) => onFormChange('expense', form, ...args)" :disabled="type.includes('freight')" v-else />
          </p-form-model-item>
        </a-col>
        <a-col :span="6">
          <p-form-model-item label="备注" prop="memo">
            <div v-if="!isFocus">{{ form.memo }}</div>
            <p-input v-model="form.memo" v-else />
          </p-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="6">
          <p-form-model-item label="计费模式" prop="settlementModel">
            <div v-if="!isFocus">{{ form.settlementModelName }}</div>
            <p-select v-model="form.settlementModel" :options="options.SETTLEMENT_MODEL" @change="settlementModelChange" v-else />
          </p-form-model-item>
        </a-col>
        <a-col :span="6" v-if="form.settlementModel === 1 || form.settlementModel === 8">
          <p-form-model-item label="阶梯方式" prop="ladderType">
            <div v-if="!isFocus">{{ form.ladderTypeName }}</div>
            <p-select v-model="form.ladderType" :options="options.LADDER_TYPE" @change="ladderTypeChange" v-else :disabled="form.settlementModel === 8" />
          </p-form-model-item>
        </a-col>
        <a-col :span="6" v-if="form.settlementModel === 1 || form.settlementModel === 6 || form.settlementModel === 8">
          <p-form-model-item label="计费方式" prop="settlementType">
            <div v-if="!isFocus">{{ form.settlementTypeName }}</div>
            <p-select v-model="form.settlementType" :options="settlementTypeEnums" @change="settlementTypeChange" v-else :disabled="form.settlementModel === 8" />
          </p-form-model-item>
        </a-col>
        <a-col :span="6" v-if="[2, 3, 4, 6].includes(form.settlementModel)">
          <p-form-model-item :label="form.settlementModel === 2 || form.settlementModel === 3 ? '一口价' : '单价'" prop="price">
            <div v-if="!isFocus">{{ form.price }}</div>
            <p-input v-model="form.price" suffix="元" v-else />
          </p-form-model-item>
        </a-col>
        <a-col :span="6" v-if="form.settlementModel === 3">
          <p-form-model-item label="合并策略" prop="mergeStrategyName">
            <div v-if="!isFocus">{{ form.mergeStrategyName }}</div>
            <p-search-modal v-model="form.mergeStrategyName" v-bind="$data.$formItems.mergeStrategyAttr" @change="(...args) => onFormChange('mergeStrategy', form, ...args)" v-else />
          </p-form-model-item>
        </a-col>
        <a-col :span="6" v-if="form.settlementModel === 5">
          <p-form-model-item label="比例策略" prop="proportionStrategyName">
            <div v-if="!isFocus">{{ form.proportionStrategyName }}</div>
            <p-search-modal v-model="form.proportionStrategyName" v-bind="$data.$formItems.proportionStrategyAttr" @change="(...args) => onFormChange('proportionStrategy', form, ...args)" v-else />
          </p-form-model-item>
        </a-col>
        <a-col :span="6" v-if="[3, 5, 6].includes(form.settlementType)">
          <p-form-model-item label="时间策略" prop="timeStrategyName">
            <div v-if="!isFocus">{{ form.timeStrategyName }}</div>
            <p-search-modal v-model="form.timeStrategyName" v-bind="$data.$formItems.timeStrategyAttr" @change="(...args) => onFormChange('timeStrategy', form, ...args)" v-else />
          </p-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="6">
          <p-form-model-item label="计费单据" prop="settlementOrderType">
            <div v-if="!isFocus">{{ form.settlementOrderTypeName }}</div>
            <p-select v-model="form.settlementOrderType" :options="options.SETTLEMENT_ORDER_TYPE" v-else :disabled="form.settlementModel === 8" />
          </p-form-model-item>
        </a-col>
      </a-row>
      <a-divider orientation="left">匹配条件</a-divider>
      <div>
        <p-button type="primary" @clickSync="onAddBtn" v-if="isFocus">新增</p-button>
        <a-row v-for="(item, index) in conditionList" :key="index" style="margin-top: 10px">
          <a-col :span="6">
            <p-select v-model="item.conditionType" :options="BMS_APPROVAL_CONDITION_TYPE" allowClear @change="(value) => approvalConditionTypeChange(value, item)" :disabled="!isFocus" />
          </a-col>
          <a-col :span="6" style="margin-left: 10px" v-show="![8, 9, 10, 11, 12].includes(item.conditionType)">
            <p-select v-model="item.conditionValue" :options="approvalConditionValueEnums[item.conditionType]" :disabled="!isFocus" @change="(...args) => conditionValueChange(item, ...args)" />
          </a-col>
          <a-col :span="6" style="margin-left: 10px" v-show="(item.conditionType === 2 || item.conditionType === 3) && item.conditionValue === 1">
            <p-search-modal v-model="item.unitName" v-bind="customerAttr" @change="(...args) => onFormChange('customer', item, ...args)" :disabled="!isFocus" />
          </a-col>
          <a-col :span="6" style="margin-left: 10px" v-show="(item.conditionType === 2 || item.conditionType === 3) && item.conditionValue === 2">
            <p-search-modal v-model="item.unitName" v-bind="branchAttr" @change="(...args) => onFormChange('branch', item, ...args)" :disabled="!isFocus" />
          </a-col>
          <a-col :span="6" style="margin-left: 10px" v-show="(item.conditionType === 2 || item.conditionType === 3) && item.conditionValue === 3">
            <p-search-modal v-model="item.unitName" v-bind="supplierAttr" @change="(...args) => onFormChange('supplier', item, ...args)" :disabled="!isFocus" />
          </a-col>
          <a-col :span="6" style="margin-left: 10px" v-show="item.conditionType === 8">
            <p-search-modal v-model="item.unitName" v-bind="consignorAttr" @change="(...args) => onFormChange('consignor', item, ...args)" :disabled="!isFocus" />
          </a-col>
          <a-col :span="6" style="margin-left: 10px" v-show="item.conditionType === 9">
            <departmenTree :departmentId="departmentId" @getDepartment="(value) => getDepartment(value, item)" :disabled="!isFocus"></departmenTree>
          </a-col>
          <a-col :span="6" style="margin-left: 10px" v-show="item.conditionType === 10">
            <p-search-modal v-model="item.unitName" v-bind="itemAttr" @change="(...args) => onFormChange('item', item, ...args)" :disabled="!isFocus" />
          </a-col>
          <a-col :span="6" style="margin-left: 10px" v-show="item.conditionType === 11">
            <p-search-modal v-model="item.unitName" v-bind="categoryAttr" @change="(...args) => onFormChange('category', item, ...args)" :disabled="!isFocus" />
          </a-col>
          <a-col :span="6" style="margin-left: 10px" v-show="item.conditionType === 12">
            <p-search-modal v-model="item.unitName" v-bind="vehicleTypeAttr" @change="(...args) => onFormChange('vehicleType', item, ...args)" :disabled="!isFocus" />
          </a-col>
          <a-col :span="2" style="margin-left: 10px">
            <a-space>
              <p-button type="danger" @click="onDelBtn(index)" v-if="isFocus && form.approvalFlowType !== 1">删除</p-button>
            </a-space>
          </a-col>
        </a-row>
      </div>
    </p-form-model>
    <div v-if="form.settlementModel === 1 || form.settlementModel === 8">
      <a-divider orientation="left">阶梯计费</a-divider>
      <p-card>
        <a-space slot="extra">
          <p-button type="primary" @clickSync="onTableAddBtn" v-if="isFocus">新增</p-button>
          <p-button type="danger" @clickSync="onTableDeleteBtn" v-if="isFocus" :disabled="!selectedRowKeys.length">删除</p-button>
        </a-space>
        <t-table
          ref="priceTable"
          dragle
          bordered
          row-key="id"
          :scroll="{ x: true }"
          :vScrollX="{ enabled: false }"
          :vScrollY="{ enabled: true, rows: 4 }"
          :data-source.sync="ladderList"
          :rowSelectType="2"
          @selectChange="(selectKey, selectData) => (selectedRowKeys = selectKey)"
          :autoLoad="false"
        >
          <t-table-column data-index="sectionDownSymbol" title="区间下限符号">
            <template slot-scope="text, record">
              <div v-if="!isFocus">{{ record.sectionDownSymbolName }}</div>
              <p-select v-model="record.sectionDownSymbol" :options="options.SECTION_SYMBOL" style="width: 100%" v-else />
            </template>
          </t-table-column>
          <t-table-column data-index="sectionDown" title="区间下限">
            <template slot-scope="text, record">
              <div v-if="!isFocus">{{ record.sectionDown }}</div>
              <p-input-number v-model="record.sectionDown" v-else />
            </template>
          </t-table-column>
          <t-table-column data-index="sectionUpSymbol" title="区间上限符号">
            <template slot-scope="text, record">
              <div v-if="!isFocus">{{ record.sectionUpSymbolName }}</div>
              <p-select v-model="record.sectionUpSymbol" :options="options.LESS_THAN" style="width: 100%" v-else />
            </template>
          </t-table-column>
          <t-table-column data-index="sectionUp" title="区间上限">
            <template slot-scope="text, record">
              <div v-if="!isFocus">{{ record.sectionUp }}</div>
              <p-input-number v-model="record.sectionUp" :min="record.sectionDown" v-else />
            </template>
          </t-table-column>
          <t-table-column data-index="initialLadder" :title="form.ladderType === 1 ? '首重(KG)' : '首方(m³)'" v-if="form.ladderType !== 2">
            <template slot-scope="text, record">
              <div v-if="!isFocus">{{ record.initialLadder }}</div>
              <p-input-number v-model="record.initialLadder" :min="0" v-else />
            </template>
          </t-table-column>
          <p-table-column data-index="initialLadderPrice" title="首重单价(元)" v-if="form.settlementModel === 8">
            <template slot-scope="text, record">
              <div v-if="!isFocus">{{ record.initialLadderPrice }}</div>
              <p-input-number v-model="record.initialLadderPrice" :min="0" v-else />
            </template>
          </p-table-column>
          <p-table-column data-index="startingPrice" title="起步价(元)" v-if="form.ladderType === 1">
            <template slot-scope="text, record">
              <div v-if="!isFocus">{{ record.startingPrice }}</div>
              <p-input-number v-model="record.startingPrice" :min="0" v-else />
            </template>
          </p-table-column>
          <t-table-column data-index="price" :title="form.settlementModel === 8 ? '续重单价(元)' : '单价(元)'">
            <template slot-scope="text, record">
              <div v-if="!isFocus">{{ record.price }}</div>
              <p-input-number v-model="record.price" :min="0" v-else />
            </template>
          </t-table-column>
          <p-table-column data-index="calculateRule" title="计算规则" v-if="form.settlementModel === 8">
            <template slot-scope="text, record">
              <div v-if="!isFocus">{{ record.calculateRuleName }}</div>
              <p-select v-model="record.calculateRule" :options="options.CALCULATE_RULE" style="width: 100%" v-else />
            </template>
          </p-table-column>
          <t-table-column data-index="ladderType" title="类别">
            <template slot-scope="text, record">
              <div v-if="!isFocus">{{ record.ladderType }}</div>
              <p-input v-model="record.ladderType" v-else />
            </template>
          </t-table-column>
        </t-table>
      </p-card>
    </div>
  </div>
</template>

<script>
import * as $formItems from '@/blocks/components/searchModalConfig/index';
import departmenTree from '@/blocks/components/formConfig/index.departmenTree.multiple.vue';
import CascaderZone from '@/blocks/components/cascaderZone';

import { priceView } from '../js/api.js';

export default {
  components: { PCascaderZone: CascaderZone, departmenTree },
  props: {
    type: {
      type: String,
      default: '',
    },
    priceId: {
      type: String,
      default: '',
    },
    options: {
      type: Object,
      default: () => ({}),
    },
    // 合同信息
    contractInfo: {
      type: Object,
      default: () => ({}),
    },
  },
  computed: {
    isFocus() {
      return this.type !== 'freightView' && this.type !== 'surchargeView';
    },
    customerAttr() {
      let customerAttr = $formItems.customerAttr;
      customerAttr.tableConfig.rowSelectType = 2;
      return customerAttr;
    },
    branchAttr() {
      let branchAttr = $formItems.branchAttr;
      branchAttr.tableConfig.rowSelectType = 2;
      return branchAttr;
    },
    supplierAttr() {
      let supplierAttr = $formItems.supplierNoConsignorAttr;
      supplierAttr.tableConfig.rowSelectType = 2;
      return supplierAttr;
    },
    consignorAttr() {
      let consignorAttr = $formItems.consignorAttr;
      consignorAttr.tableConfig.rowSelectType = 2;
      return consignorAttr;
    },
    expenseDriverAttr() {
      let expenseDriverAttr = $formItems.expenseDriverAttr;
      return Object.assign({ remoteData: { excludeExpenseName: '运费', expenseClassify: 2, status: 1 } }, expenseDriverAttr);
    },
    itemAttr() {
      let itemAttr = $formItems.itemAttr;
      itemAttr.tableConfig.rowSelectType = 2;
      return itemAttr;
    },
    categoryAttr() {
      let categoryAttr = $formItems.categoryAttr;
      categoryAttr.tableConfig.rowSelectType = 2;
      return categoryAttr;
    },
    vehicleTypeAttr() {
      let vehicleTypeAttr = $formItems.vehicleTypeAttr;
      vehicleTypeAttr.tableConfig.rowSelectType = 2;
      return vehicleTypeAttr;
    },
  },
  data() {
    const positveOrZero = (rule, value, callback) => {
      if (value && !/^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/.test(value)) {
        callback(new Error('请输入非负数'));
      } else {
        callback();
      }
    };
    return {
      $formItems,

      form: {
        priceNo: '',
        priceName: '',
        shipperProvinceName: '',
        consigneeProvinceName: '',
        aging: '',
        mileage: '',
        expenseName: '',
        memo: '',
        settlementModel: '',
        ladderType: '',
        settlementType: '',
        price: '',
        mergeStrategyName: '',
        proportionStrategyName: '',
        timeStrategyName: '',
      },
      rules: {
        priceNo: [{ required: true, message: '价格编号不能为空', trigger: 'blur' }],
        priceName: [{ required: true, message: '价格名称不能为空', trigger: 'blur' }],
        // shipperProvinceName: [{ required: true, message: '始发地不能为空', trigger: 'change' }],
        // consigneeProvinceName: [{ required: true, message: '目的地不能为空', trigger: 'change' }],
        expenseName: [{ required: true, message: '费用不能为空', trigger: 'change' }],
        settlementModel: [{ required: true, message: '计费模式不能为空', trigger: 'change' }],
        settlementType: [{ required: true, message: '计费方式不能为空', trigger: 'change' }],
        price: [{ validator: positveOrZero, trigger: 'blur' }],
      },
      // 省市区弹窗状态
      shipperZoneValue: [],
      consigneeZoneValue: [],
      // 阶梯方式
      LADDER_TYPE: [],
      // 计费方式
      settlementTypeEnums: [],
      // 计费单据
      SETTLEMENT_ORDER_TYPE: [],

      // 匹配条件
      conditionList: [],
      approvalConditionValueEnums: {
        '': [],
        1: this.options.DELIVERY_METHOD_TYPE, // 配送方式
        2: this.options.SHIPPER_TYPE, // 发货商类型
        3: this.options.CONSIGNEE_TYPE, // 收货方类型
        4: this.options.ORDER_TYPE, // 订单类型
        5: this.options.ORDER_BUSINESS_TYPE, // 订单业务类型
        6: this.options.TRANSPORT_TYPE, // 运输类型
        7: this.options.TEMPERATURE_REQUIREMENT, // 温度要求
        8: [], // 货主
        9: [], // 部门
      },
      BMS_APPROVAL_CONDITION_TYPE: [],
      departmentId: '', // 部门id

      // 阶梯计费
      ladderList: [],
      selectedRowKeys: [],
    };
  },
  mounted() {
    this.BMS_APPROVAL_CONDITION_TYPE = this.options.BMS_APPROVAL_CONDITION_TYPE.map((item) => {
      item.disabled = false;
      return item;
    });
    if (this.type.includes('freight')) {
      this.form.expenseName = '支出运费';
    }
    if (this.priceId) {
      this.spinning = true;
      priceView({ id: this.priceId })
        .then((res) => {
          if (res.success) {
            this.form = { ...res.data };
            this.conditionList = res.data.conditionList.map((item) => {
              if (![8, 9, 10, 11, 12].includes(item.conditionType)) {
                item.conditionValue = Number(item.conditionValue);
              }
              return { ...item };
            });
            this.ladderList = [...res.data.ladderList];
            delete this.form.conditionList;
            delete this.form.ladderList;
            if (this.form.shipperProvince) {
              this.shipperZoneValue.push(this.form.shipperProvince);
            }
            if (this.form.shipperCity) {
              this.shipperZoneValue.push(this.form.shipperCity);
            }
            if (this.form.shipperDistrict) {
              this.shipperZoneValue.push(this.form.shipperDistrict);
            }
            if (this.form.consigneeProvince) {
              this.consigneeZoneValue.push(this.form.consigneeProvince);
            }
            if (this.form.consigneeCity) {
              this.consigneeZoneValue.push(this.form.consigneeCity);
            }
            if (this.form.consigneeDistrict) {
              this.consigneeZoneValue.push(this.form.consigneeDistrict);
            }
            this.settlementTypeEnums =
              this.form.ladderType === 1
                ? this.options.SETTLEMENT_TYPE_KG
                : this.form.ladderType === 2
                ? this.options.SETTLEMENT_TYPE_MILEAGE
                : this.form.ladderType === 3
                ? this.options.SETTLEMENT_TYPE_M3
                : this.form.settlementModel === 6
                ? this.options.SETTLEMENT_TYPE_TIME
                : [];
            this.APPROVAL_CONDITION_TYPE_INIT();
            // 处理部门渲染
            this.$nextTick(() => {
              this.conditionList.forEach((item) => {
                if (item.conditionType === 9) {
                  this.departmentId = item.unitId;
                }
              });
            });
          } else {
            this.$exception(res);
          }
        })
        .finally(() => {
          this.spinning = false;
        });
    }
  },
  methods: {
    // 省市区
    shipperZoneChange(value, arrValue, form) {
      this.form.shipperProvinceName = '';
      this.form.shipperProvince = '';
      this.form.shipperCityName = '';
      this.form.shipperCity = '';
      this.form.shipperDistrictName = '';
      this.form.shipperDistrict = '';
      this.shipperZoneValue = value;
      if (arrValue && arrValue.length) {
        if (arrValue[0]) {
          this.form.shipperProvinceName = arrValue[0].zname;
          this.form.shipperProvince = arrValue[0].zoneid;
        }
        if (arrValue[1]) {
          this.form.shipperCityName = arrValue[1].zname;
          this.form.shipperCity = arrValue[1].zoneid;
        }
        if (arrValue[2]) {
          this.form.shipperDistrictName = arrValue[2].zname;
          this.form.shipperDistrict = arrValue[2].zoneid;
        }
      }
    },
    consigneeZoneChange(value, arrValue, form) {
      this.form.consigneeProvinceName = '';
      this.form.consigneeProvince = '';
      this.form.consigneeCityName = '';
      this.form.consigneeCity = '';
      this.form.consigneeDistrictName = '';
      this.form.consigneeDistrict = '';
      this.consigneeZoneValue = value;
      if (arrValue && arrValue.length) {
        if (arrValue[0]) {
          this.form.consigneeProvinceName = arrValue[0].zname;
          this.form.consigneeProvince = arrValue[0].zoneid;
        }
        if (arrValue[1]) {
          this.form.consigneeCityName = arrValue[1].zname;
          this.form.consigneeCity = arrValue[1].zoneid;
        }
        if (arrValue[2]) {
          this.form.consigneeDistrictName = arrValue[2].zname;
          this.form.consigneeDistrict = arrValue[2].zoneid;
        }
      }
    },

    ladderTypeChange(value) {
      let settlementTypeEnums = {
        1: this.options.SETTLEMENT_TYPE_KG,
        2: this.options.SETTLEMENT_TYPE_MILEAGE,
        3: this.options.SETTLEMENT_TYPE_M3,
        '': [],
      };
      this.settlementTypeEnums = settlementTypeEnums[value];
      this.form.settlementType = '';
      this.ladderList.forEach((item) => {
        item.initialLadder = '';
        item.startingPrice = '';
      });
    },

    // ----------------------------------匹配条件----------------------------------
    onAddBtn() {
      this.conditionList.push({
        conditionType: '',
        conditionValue: '',
        conditionName: '',
        unitId: '',
        unitCode: '',
        unitName: '',
      });
    },
    onDelBtn(index) {
      this.conditionList.splice(index, 1);
      this.APPROVAL_CONDITION_TYPE_INIT();
    },
    approvalConditionTypeChange(value, item) {
      item.conditionValue = '';
      item.conditionName = '';
      item.unitId = '';
      item.unitCode = '';
      item.unitName = '';
      this.APPROVAL_CONDITION_TYPE_INIT();
    },
    // 更新APPROVAL_CONDITION_TYPE
    APPROVAL_CONDITION_TYPE_INIT() {
      let selectList = this.conditionList.map((item) => item.conditionType);
      this.BMS_APPROVAL_CONDITION_TYPE.forEach((item) => {
        if (selectList.includes(item.value)) {
          item.disabled = true;
        } else {
          item.disabled = false;
        }
      });
    },
    // 部门change
    getDepartment(value, item) {
      this.departmentId = value.departmentId;
      item.unitId = value.departmentId;
      item.unitCode = value.departmentCode;
      item.unitName = value.departmentName;
    },
    onFormChange(name, item, ...args) {
      if (!args[1]) {
        this.$set(item, 'unitId', '');
        this.$set(item, 'unitCode', '');
        this.$set(item, 'unitName', '');
        return;
      }
      let idList = [],
        codeList = [];
      if (name === 'consignor') {
        args[2].forEach((item) => {
          idList.push(item.cargoOwnerId);
          codeList.push(item.cargoOwnerCode);
        });
        this.$set(item, 'unitId', idList.join(','));
        this.$set(item, 'unitCode', codeList.join(','));
        this.$set(item, 'unitName', args[1]);
      } else if (name === 'expense') {
        this.$set(item, 'expenseId', args[1] ? args[2][0].id : '');
        this.$set(item, 'expenseName', args[1]);
      } else if (name === 'mergeStrategy') {
        this.$set(item, 'mergeStrategyId', args[1] ? args[2][0].id : '');
        this.$set(item, 'mergeStrategyName', args[1]);
      } else if (name === 'proportionStrategy') {
        this.$set(item, 'proportionStrategyId', args[1] ? args[2][0].id : '');
        this.$set(item, 'proportionStrategyName', args[1]);
      } else if (name === 'timeStrategy') {
        this.$set(item, 'timeStrategyId', args[1] ? args[2][0].id : '');
        this.$set(item, 'timeStrategyName', args[1]);
      } else if (name === 'item') {
        args[2].forEach((item) => {
          idList.push(item.itemId);
          codeList.push(item.itemCode);
        });
        this.$set(item, 'unitId', idList.join(','));
        this.$set(item, 'unitCode', codeList.join(','));
        this.$set(item, 'unitName', args[1]);
      } else if (name === 'category') {
        args[2].forEach((item) => {
          idList.push(item.itemCategoryId);
          codeList.push(item.itemCategoryCode);
        });
        this.$set(item, 'unitId', idList.join(','));
        this.$set(item, 'unitCode', codeList.join(','));
        this.$set(item, 'unitName', args[1]);
      } else {
        args[2].forEach((item) => {
          idList.push(item.id);
          codeList.push(item.code);
        });
        this.$set(item, 'unitId', idList.join(','));
        this.$set(item, 'unitCode', codeList.join(','));
        this.$set(item, 'unitName', args[1]);
      }
    },

    // ----------------------------------表格----------------------------------
    // 新增
    onTableAddBtn() {
      this.ladderList.push({
        id: new Date().getTime(),
        type: 'add',
      });
    },
    // 删除
    onTableDeleteBtn() {
      let list = [];
      this.ladderList.forEach((item) => {
        if (!this.selectedRowKeys.includes(item.id)) {
          list.push(item);
        }
      });
      this.ladderList = [...list];
      this.selectedRowKeys = [];
    },

    // 修改计费模式重置数据
    settlementModelChange(value) {
      this.form.ladderType = '';
      this.form.settlementType = '';
      this.ladderList = [];
      this.form.price = '';
      this.form.mergeStrategyId = '';
      this.form.mergeStrategyName = '';
      this.form.proportionStrategyId = '';
      this.form.proportionStrategyName = '';
      this.settlementTypeEnums = value === 6 ? this.options.SETTLEMENT_TYPE_TIME : [];
      this.form.timeStrategyId = '';
      this.form.timeStrategyName = '';

      if (value === 8) {
        this.form.ladderType = 1;
        this.form.settlementType = 2;
        this.form.settlementOrderType = 3;
        this.settlementTypeEnums = this.options.SETTLEMENT_TYPE_KG;
      }
    },

    conditionValueChange(item, ...args) {
      item.conditionName = args[1];
    },

    settlementTypeChange() {
      this.form.timeStrategyId = '';
      this.form.timeStrategyName = '';
    },
  },
};
</script>

<style lang="less" scoped>
.ant-input-number {
  width: 100%;
}
</style>