<!--
@name: 流程配置-编辑页
@author: 张泽贤
@time: 2022-06-22
@doc: PROLOG_UPCloudSolutionForTMS__v1.2.1_基础配置_1.2.2
-->
<template>
    <p-card class="full-card-panel" :bordered="false">
      <a-space slot="title" style="cursor: pointer">
        <a-icon type="arrow-left" style="color: #1990ff" @click="back" />
        {{ flowType === 1 ? '承运商' : '' }}流程配置-{{ typeTitle }}
      </a-space>
        <p-button slot="extra" type="primary" v-show="type === 'add' || type === 'edit'" @click="onSave">保存</p-button>

        <p-spin :spinning="spinning">

        <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="flowName">
                <p-input v-model="form.flowName" :disabled="type !== 'add'" />
              </p-form-model-item>
            </a-col>
            <a-col :span="6">
              <p-form-model-item label="优先级" prop="priorityLevel">
                <p-input-number v-model="form.priorityLevel" :min="1" :precision="0" :disabled="type === 'view'" />
              </p-form-model-item>
            </a-col>
          </a-row>

          <a-divider orientation="left">满足条件</a-divider>
          <a-row v-for="(item, index) in flowConditions" :key="item.index">
            <a-col :span="6">
              <p-form-model-item label="" prop="flowConditionType" :label-col="{ span: 0 }" :wrapper-col="{ span: 24 }">
                <p-select
                  v-model="item.flowConditionType"
                  :options="FLOW_CONDITION_TYPE_flowConditions"
                  allowClear
                  @change="(value) => flowConditionTypeChange(value, item, 'flowConditions')"
                  :disabled="index === 0 || type === 'view'"
                />
              </p-form-model-item>
            </a-col>
            <a-col :span="6" style="margin-left: 10px" v-show="item.flowConditionType !== 8">
              <p-form-model-item label="" prop="flowConditionValue" :label-col="{ span: 0 }" :wrapper-col="{ span: 24 }">
                <p-select
                  v-model="item.flowConditionValue"
                  :options="flowConditionValueEnums[item.flowConditionType]"
                  :allowClear="index !== 0"
                  @change="(value) => flowConditionValueChange(value, item)"
                  :disabled="type === 'view'"
                  :mode="modeEnums[item.flowConditionType]"
                />
              </p-form-model-item>
            </a-col>
            <a-col :span="6" style="margin-left: 10px" v-show="(item.flowConditionType === 2 || item.flowConditionType === 3) && item.flowConditionValue === 1">
              <p-form-model-item label="" prop="unitName" :label-col="{ span: 0 }" :wrapper-col="{ span: 24 }">
                <p-search-modal v-model="item.unitName" v-bind="customerAttr" @change="(...args) => onFormChange('customer', item, ...args)" :disabled="type === 'view'" />
              </p-form-model-item>
            </a-col>
            <a-col :span="6" style="margin-left: 10px" v-show="(item.flowConditionType === 2 || item.flowConditionType === 3) && item.flowConditionValue === 2">
              <p-form-model-item label="" prop="unitName" :label-col="{ span: 0 }" :wrapper-col="{ span: 24 }">
                <p-search-modal v-model="item.unitName" v-bind="branchAttr" @change="(...args) => onFormChange('branch', item, ...args)" :disabled="type === 'view'" />
              </p-form-model-item>
            </a-col>
            <a-col :span="6" style="margin-left: 10px" v-show="(item.flowConditionType === 2 || item.flowConditionType === 3) && item.flowConditionValue === 3">
              <p-form-model-item label="" prop="unitName" :label-col="{ span: 0 }" :wrapper-col="{ span: 24 }">
                <p-search-modal v-model="item.unitName" v-bind="supplierAttr" @change="(...args) => onFormChange('supplier', item, ...args)" :disabled="type === 'view'" />
              </p-form-model-item>
            </a-col>
            <a-col :span="6" style="margin-left: 10px" v-show="item.flowConditionType === 8">
              <p-form-model-item label="" prop="unitName" :label-col="{ span: 0 }" :wrapper-col="{ span: 24 }">
                <p-search-modal v-model="item.unitName" v-bind="consignorAttr" @change="(...args) => onFormChange('consignor', item, ...args)" :disabled="type === 'view'" />
              </p-form-model-item>
            </a-col>
            <a-col :span="2">
              <p-form-model-item style="text-align: right">
                <a-space>
                  <p-button type="primary" v-if="index === 0 && type !== 'view'" @clickSync="onAddBtn('flowConditions')">新增</p-button>
                  <p-button type="danger" v-if="index !== 0 && type !== 'view'" @click="onDelBtn(index, 'flowConditions')">删除</p-button>
                </a-space>
              </p-form-model-item>
            </a-col>
          </a-row>

          <a-divider orientation="left">排除条件</a-divider>
          <a-row>
            <a-col :span="6">
              <a-radio-group v-model="excludedConditions" @change="excludedConditionsChange">
                <a-radio :value="true">满足以下条件</a-radio>
                <a-radio :value="false">不指定条件</a-radio>
              </a-radio-group>
            </a-col>
            <a-col :span="6">
              <a-space>
                <p-button type="primary" v-if="type !== 'view'" :disabled="!excludedConditions" @clickSync="onAddBtn('excludedFlowConditions')">新增</p-button>
              </a-space>
            </a-col>
          </a-row>
          <a-row v-for="(item, index1) in excludedFlowConditions" :key="index1">
            <a-col :span="6">
              <p-form-model-item label="" prop="flowConditionType" :label-col="{ span: 0 }" :wrapper-col="{ span: 24 }">
                <p-select
                  v-model="item.flowConditionType"
                  :options="FLOW_CONDITION_TYPE_excludedFlowConditions"
                  allowClear
                  @change="(value) => flowConditionTypeChange(value, item, 'excludedFlowConditions')"
                  :disabled="type === 'view'"
                />
              </p-form-model-item>
            </a-col>
            <a-col :span="6" style="margin-left: 10px" v-show="item.flowConditionType !== 8">
              <p-form-model-item label="" prop="flowConditionValue" :label-col="{ span: 0 }" :wrapper-col="{ span: 24 }">
                <p-select
                  v-model="item.flowConditionValue"
                  :options="flowConditionValueEnums[item.flowConditionType]"
                  allowClear
                  @change="(value) => flowConditionValueChange(value, item)"
                  :disabled="type === 'view'"
                  :mode="modeEnums[item.flowConditionType]"
                />
              </p-form-model-item>
            </a-col>
            <a-col :span="6" style="margin-left: 10px" v-show="(item.flowConditionType === 2 || item.flowConditionType === 3) && item.flowConditionValue === 1">
              <p-form-model-item label="" prop="unitName" :label-col="{ span: 0 }" :wrapper-col="{ span: 24 }">
                <p-search-modal v-model="item.unitName" v-bind="customerAttr" @change="(...args) => onFormChange('customer', item, ...args)" :disabled="type === 'view'" />
              </p-form-model-item>
            </a-col>
            <a-col :span="6" style="margin-left: 10px" v-show="(item.flowConditionType === 2 || item.flowConditionType === 3) && item.flowConditionValue === 2">
              <p-form-model-item label="" prop="unitName" :label-col="{ span: 0 }" :wrapper-col="{ span: 24 }">
                <p-search-modal v-model="item.unitName" v-bind="branchAttr" @change="(...args) => onFormChange('branch', item, ...args)" :disabled="type === 'view'" />
              </p-form-model-item>
            </a-col>
            <a-col :span="6" style="margin-left: 10px" v-show="(item.flowConditionType === 2 || item.flowConditionType === 3) && item.flowConditionValue === 3">
              <p-form-model-item label="" prop="unitName" :label-col="{ span: 0 }" :wrapper-col="{ span: 24 }">
                <p-search-modal v-model="item.unitName" v-bind="supplierAttr" @change="(...args) => onFormChange('supplier', item, ...args)" :disabled="type === 'view'" />
              </p-form-model-item>
            </a-col>
            <a-col :span="6" style="margin-left: 10px" v-show="item.flowConditionType === 8">
              <p-form-model-item label="" prop="unitName" :label-col="{ span: 0 }" :wrapper-col="{ span: 24 }">
                <p-search-modal v-model="item.unitName" v-bind="consignorAttr" @change="(...args) => onFormChange('consignor', item, ...args)" :disabled="type === 'view'" />
              </p-form-model-item>
            </a-col>
            <a-col :span="2">
              <p-form-model-item style="text-align: right">
                <a-space>
                  <p-button type="danger" v-if="type !== 'view'" @click="onDelBtn(index1, 'excludedFlowConditions')">删除</p-button>
                </a-space>
              </p-form-model-item>
            </a-col>
          </a-row>

          <a-divider orientation="left">流程配置</a-divider>
          <pScrollbar >

          <div class="process">
            <div v-for="(item, index) in processEnums" :key="index">
              <div class="processTitle">{{ item.title }}</div>
              <div
                :class="{
                  processBox: true,
                  processBoxChecked: item.groupcode1 === '1'||item.checked2,
                }"
              >
                {{ item.label }}
              </div>
              <p-checkbox v-model="item.checked" :value="item.checkboxValue" v-if="item.checkboxValue" class="processCheckbox" :readOnly="type === 'view'" @change="flowConfigChange">{{
                item.checkboxLabel
              }}</p-checkbox>
              <p-checkbox v-model="item.checked2" :value="item.checkboxValue2" v-if="item.checkboxValue2" class="processCheckbox" :readOnly="type === 'view'">{{ item.checkboxLabel2 }}</p-checkbox>
              <p-checkbox v-model="item.checked3" :value="item.checkboxValue3" v-if="item.checkboxValue3" class="processCheckbox" :readOnly="type === 'view'">{{ item.checkboxLabel3 }}</p-checkbox>
            </div>

          </div>
        </pScrollbar>
        </p-form-model>

      </p-spin>

    </p-card>
</template>

<script>
import * as $formItems from '@/blocks/components/searchModalConfig/index';
import { Add, Update, View } from './api';

export default {
  props: {
    type: '',
    editId: {
      type: String,
      default: '',
    },
    flowType: '', // 1-承运商 2-自有
    options: {
      type: Object,
      default: () => ({}),
    },
  },
  computed: {
    typeTitle() {
      const dictionary = {
        add: '新增',
        edit: '编辑',
        view: '查看',
      };
      return dictionary[this.type] || this.type;
    },
    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;
    },
  },
  data() {
    return {
      $formItems,
      spinning: false,
      form: {
        flowName: '',
        priorityLevel: '',
      },
      rules: {
        flowName: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        priorityLevel: [{ required: true, message: '优先级不能为空', trigger: 'blur' }],
      },

      flowConditions: [
        {
          flowConditionType: 1,
          flowConditionValue: 1,
          unitId: '',
          unitCode: '',
          unitName: '',
        },
      ],

      excludedConditions: true,
      excludedFlowConditions: [],

      flowConditionValueEnums: {
        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: this.options.CARRIERS_TYPE,
      },
      modeEnums: {
        1: 'default',
        2: 'default',
        3: 'default',
        4: 'multiple',
        5: 'multiple',
        6: 'multiple',
        7: 'multiple',
        8: 'default',
        9: 'default',
      },
      FLOW_CONDITION_TYPE_flowConditions: [],
      FLOW_CONDITION_TYPE_excludedFlowConditions: [],

      processEnums: [], // 进度list
      processNoCheckEnums: [], // 进度中默认不选中的list

      flowConfigEnums: {
        // 需要标记和多选框同步变色的参数
        SelfPickUpVehReg: 'ifSelfPickUpVehReg',
        Appointment: 'isAppointment',
        AppointmentTakeNumAndCallNum: 'AppointmentTakeNumAndCallNum',
        TransportPlan: 'isTransportPlan',
        ArrangeGoods: 'ifArrangeGoods',
        LoadUnLoad: 'ifLoadUnLoad',
        Scanning: 'ifScanning',
        SignIn: 'isSignIn',
      },
      flowConfigList: ['isTransportPlan','isAppointment', 'ifArrangeGoods', 'ifLoadUnLoad', 'ifScanning'],

      // { title: 标题, label: 箭头名称, value: 箭头value, groupcode1: 1-箭头蓝色 2-箭头白色, checkboxValue: checkbox value, checkboxLabel: checkbox label, checked: checkbox是否默认选中 },

      // 自有流程配置
      FLOW_CONFIG_ZY: [
        {
          title: '订单管理',
          label: '新增',
          value: 'Add',
          groupcode1: '1',
          checkboxValue: 'AddCommit',
          checkboxLabel: '新增后自动提交',
          checked: false,
          checkboxValue2: 'isLineStrategyAdd',
          checkboxLabel2: '是否调用线路匹配策略',
          checked2: false,
          checkboxValue3: 'isBusinessStrategyAdd',
          checkboxLabel3: '是否调用业务规则策略',
          checked3: false,
        },
        {
          title: '订单管理',
          label: '提交',
          value: 'Commit',
          groupcode1: '1',
          checkboxValue: 'CommitReview',
          checkboxLabel: '提交后自动审核',
          checked: false,
          checkboxValue2: 'isLineStrategyCommit',
          checkboxLabel2: '是否调用线路匹配策略',
          checked2: false,
          checkboxValue3: 'isBusinessStrategyCommit',
          checkboxLabel3: '是否调用业务规则策略',
          checked3: false,
        },
        {
          title: '待审核',
          label: '审核',
          value: 'Review',
          groupcode1: '1',
          checkboxValue: 'ReviewSplit',
          checkboxLabel: '是否调用拆分策略',
          checked: false,
          checkboxValue2: 'ReviewDispatch',
          checkboxLabel2: '运单匹配唯一承运方是否自动调度',
          checked2: false,
        },



        { title: '运输计划', label: '计划', value: 'TransportPlan', groupcode1: '2', checkboxValue: 'isTransportPlan', checkboxLabel: '是否开启计划', checked: false },

        { title: '待调度', label: '调度', value: 'Dispatch', groupcode1: '1', checkboxValue: 'DispatchCheck', checkboxLabel: '调度后自动审核', checked: false },
        { title: '已派车/已托运', label: '调度审核', value: 'Check', groupcode1: '1' },
        {
          title: '待预约', label: '预约', value: 'Appointment', groupcode1: '2', checkboxValue: 'isAppointment', checkboxLabel: '是否开启预约', checked: false,
          checkboxValue2: 'AppointmentTakeNumAndCallNum',
          checkboxLabel2: '是否开启取号叫号',
          checked2: false,
        },
        { title: '待理货', label: '理货', value: 'ArrangeGoods', groupcode1: '2', checkboxValue: 'ifArrangeGoods', checkboxLabel: '是否开启理货', checked: false },
        { title: '待装卸', label: '装卸', value: 'LoadUnLoad', groupcode1: '2', checkboxValue: 'ifLoadUnLoad', checkboxLabel: '是否开启装卸', checked: false },
        { title: '待装卸', label: '装车扫描', value: 'Scanning', groupcode1: '2', checkboxValue: 'ifScanning', checkboxLabel: '是否开启装车扫描', checked: false },
        { title: '待提货/提货确认/拣货出库/整车直发', label: '启运登记', value: 'SendOff', groupcode1: '1' },
        { title: '待送达/入库在途/出库在途/整车直发', label: '送达确认', value: 'ArriveConfirm', groupcode1: '1' },
        { title: '', label: '签收', value: 'SignIn', groupcode1: '2', checkboxValue: 'isSignIn', checkboxLabel: '是否开启签收', checked: false },
      ],
      // 自提流程配置
      FLOW_CONFIG_ZT: [
        {
          title: '订单管理',
          label: '新增',
          value: 'Add',
          groupcode1: '1',
          checkboxValue: 'AddCommit',
          checkboxLabel: '新增后自动提交',
          checked: false,
          checkboxValue2: 'isLineStrategyAdd',
          checkboxLabel2: '是否调用线路匹配策略',
          checked2: false,
          checkboxValue3: 'isBusinessStrategyAdd',
          checkboxLabel3: '是否调用业务规则策略',
          checked3: false,
        },
        {
          title: '订单管理',
          label: '提交',
          value: 'Commit',
          groupcode1: '1',
          checkboxValue: 'CommitReview',
          checkboxLabel: '提交后自动审核',
          checked: false,
          checkboxValue2: 'isLineStrategyCommit',
          checkboxLabel2: '是否调用线路匹配策略',
          checked2: false,
          checkboxValue3: 'isBusinessStrategyCommit',
          checkboxLabel3: '是否调用业务规则策略',
          checked3: false,
        },
        { title: '待审核', label: '审核', value: 'Review', groupcode1: '1' },
        { title: '待自提', label: '自提车辆登记', value: 'SelfPickUpVehReg', groupcode1: '2', checkboxValue: 'ifSelfPickUpVehReg', checkboxLabel: '是否自提车辆登记', checked: false },
        { title: '待预约', label: '预约', value: 'Appointment', groupcode1: '2', checkboxValue: 'isAppointment', checkboxLabel: '是否开启预约', checked: false ,    checkboxValue2: 'AppointmentTakeNumAndCallNum',
          checkboxLabel2: '是否开启取号叫号',
          checked2: false},
        { title: '待理货', label: '理货', value: 'ArrangeGoods', groupcode1: '2', checkboxValue: 'ifArrangeGoods', checkboxLabel: '是否开启理货', checked: false },
        { title: '待装卸', label: '装卸', value: 'LoadUnLoad', groupcode1: '2', checkboxValue: 'ifLoadUnLoad', checkboxLabel: '是否开启装卸', checked: false },
        { title: '待装卸', label: '装车扫描', value: 'Scanning', groupcode1: '2', checkboxValue: 'ifScanning', checkboxLabel: '是否开启装车扫描', checked: false },
        { title: '待自提', label: '自提确认', value: 'SelfPickUpConfirm	', groupcode1: '1' },
      ],
      // 承运商流程配置
      FLOW_CONFIG_CYS: [
        {
          title: '订单管理',
          label: '接单',
          value: 'Receive',
          groupcode1: '1',
          checkboxValue: 'RejectReceive',
          checkboxLabel: '承运商是否开启接单拒绝',
          checked: false,
          checkboxValue2: 'ReceiveCommit',
          checkboxLabel2: '接单后自动提交',
          checked2: false,
        },
        { title: '订单管理', label: '提交', value: 'Commit', groupcode1: '1', checkboxValue: 'CommitReview', checkboxLabel: '提交后自动审核', checked: false },
        // { title: '待审核', label: '审核', value: 'Review', groupcode1: '1', checkboxValue: 'ReviewDispatch', checkboxLabel: '运单匹配唯一承运方是否自动调度', checked: false },
        { title: '待审核', label: '审核', value: 'Review', groupcode1: '1' },
        { title: '待调度', label: '调度', value: 'Dispatch', groupcode1: '1', checkboxValue: 'DispatchCheck', checkboxLabel: '调度后自动审核', checked: false },
        { title: '已派车/已托运', label: '调度审核', value: 'Check', groupcode1: '1', checkboxValue: '', checkboxLabel: '' },
        { title: '待预约', label: '预约', value: 'Appointment', groupcode1: '2', checkboxValue: 'isAppointment', checkboxLabel: '是否开启预约', checked: false,    checkboxValue2: 'AppointmentTakeNumAndCallNum',
          checkboxLabel2: '是否开启取号叫号',checked2: false },
        { title: '待理货', label: '理货', value: 'ArrangeGoods', groupcode1: '2', checkboxValue: 'ifArrangeGoods', checkboxLabel: '是否开启理货', checked: false },
        { title: '待装卸', label: '装卸', value: 'LoadUnLoad', groupcode1: '2', checkboxValue: 'ifLoadUnLoad', checkboxLabel: '是否开启装卸', checked: false },
        { title: '待装卸', label: '装车扫描', value: 'Scanning', groupcode1: '2', checkboxValue: 'ifScanning', checkboxLabel: '是否开启装车扫描', checked: false },
        { title: '待提货/提货确认/拣货出库/整车直发', label: '启运登记', value: 'SendOff', groupcode1: '1' },
        { title: '待送达/入库在途/出库在途/整车直发', label: '送达确认', value: 'ArriveConfirm', groupcode1: '1' },
        { title: '', label: '签收', value: 'SignIn', groupcode1: '2', checkboxValue: 'isSignIn', checkboxLabel: '是否开启签收', checked: false },
      ],
    };
  },
  mounted() {
    this.FLOW_CONDITION_TYPE_flowConditions = this.options.FLOW_CONDITION_TYPE.map((item) => {
      if (item.value === 1) {
        item.disabled = true;
      } else {
        item.disabled = false;
      }
      return { ...item };
    });
    this.FLOW_CONDITION_TYPE_excludedFlowConditions = this.options.FLOW_CONDITION_TYPE.map((item) => {
      item.disabled = false;
      return { ...item };
    });
    let flowType = this.flowType === 1 ? 'FLOW_CONFIG_CYS' : 'FLOW_CONFIG_ZY';
    this.processEnums = this[flowType].map((item) => {
      return { ...item };
    });

    // 查询明细
    if (this.editId) {
      this.spinning = true;
      View({ id: this.editId })
        .then((res) => {
          if (res.success) {
            this.form.flowName = res.data.flowName;
            this.form.priorityLevel = res.data.priorityLevel;
            this.flowConditions = [...res.data.businessFlowConditionViewDTOs].map((item) => {
              if ([4, 5, 6, 7].includes(item.flowConditionType)) {
                item.flowConditionValue = item.flowConditionValue.split(',').map((item) => Number(item));
              } else {
                item.flowConditionValue = Number(item.flowConditionValue);
              }
              return item;
            });

            this.excludedFlowConditions = [...res.data.excludedBusinessFlowConditionViewDTOs].map((item) => {
              if ([4, 5, 6, 7].includes(item.flowConditionType)) {
                item.flowConditionValue = item.flowConditionValue.split(',').map((item) => Number(item));
              } else {
                item.flowConditionValue = Number(item.flowConditionValue);
              }
              return item;
            });
            this.excludedConditions = this.excludedFlowConditions.length > 0;

            let flowConfigList = res.data.flowConfig.split(',');
            let flowConfigEnumsKeyList = Object.getOwnPropertyNames(this.flowConfigEnums);
            let selectList = [];
            flowConfigList.forEach((item) => {
              if (flowConfigEnumsKeyList.includes(item)) {
                selectList.push(item);
              }
            });

            if (this.flowType === 2 && this.flowConditions[0].flowConditionValue === 2) {
              this.processEnums = this['FLOW_CONFIG_ZT'].map((item) => {
                return { ...item };
              });
            }

            this.processEnums.forEach((item) => {
              if (flowConfigList.includes(item.checkboxValue) || selectList.includes(item.value)) {
                item.groupcode1 = '1';
                item.checked = true;
              }
              if (flowConfigList.includes(item.checkboxValue2)) {
                item.checked2 = true;
              }
              if (flowConfigList.includes(item.checkboxValue3)) {
                item.checked3 = true;
              }
            });

            this.FLOW_CONDITION_TYPE_INIT('flowConditions');
            this.FLOW_CONDITION_TYPE_INIT('excludedFlowConditions');
          } else {
            this.$exception(res);
          }
        })
        .finally(() => {
          this.spinning = false;
        });
    }
  },
  methods: {
    back() {
      this.$emit('back');
    },
    // 保存
    onSave() {
      this.$refs.ruleForm.validate((valid) => {
        if (!valid) return;

        let flowConfig = [];
        this.processEnums.forEach((item, index) => {
          if (item.groupcode1 === '1') {
            flowConfig.push(item.value);
          }
          // if/is对应的字段不传，但是回显要勾上
          if (!!item.checked && item.checkboxValue && item.checkboxValue.substring(0, 2) !== 'if' && item.checkboxValue.substring(0, 2) !== 'is') {
            flowConfig.push(item.checkboxValue);
          }
          if (item.checked2) {
            flowConfig.push(item.checkboxValue2);
          }
          if (item.checked3) {
            flowConfig.push(item.checkboxValue3);
          }
        });
        let flowConditions = [];
        for (let index = 0; index < this.flowConditions.length; index++) {
          const item = { ...this.flowConditions[index] };
          if (!item.flowConditionType) {
            this.$message.error('请选择满足条件-流程配置条件');
            return false;
          }
          if ([4, 5, 6, 7].includes(item.flowConditionType)) {
            item.flowConditionValue = item.flowConditionValue.join(',');
          }
          flowConditions.push(item);
        }
        // let flowConditions = this.flowConditions.map((item) => {
        //   item = { ...item };
        //   if ([4, 5, 6, 7].includes(item.flowConditionType)) {
        //     item.flowConditionValue = item.flowConditionValue.join(',');
        //   }
        //   return item;
        // });

        let excludedFlowConditions = [];
        for (let index = 0; index < this.excludedFlowConditions.length; index++) {
          const item = { ...this.excludedFlowConditions[index] };
          if (!item.flowConditionType) {
            this.$message.error('请选择排除条件-流程配置条件');
            return false;
          }
          if ([4, 5, 6, 7].includes(item.flowConditionType)) {
            item.flowConditionValue = item.flowConditionValue.join(',');
          }
          excludedFlowConditions.push(item);
        }
        // let excludedFlowConditions = this.excludedFlowConditions.map((item) => {
        //   item = { ...item };
        //   if ([4, 5, 6, 7].includes(item.flowConditionType)) {
        //     item.flowConditionValue = item.flowConditionValue.join(',');
        //   }
        //   return item;
        // });

        let request = {
          id: this.editId,
          flowName: this.form.flowName,
          priorityLevel: this.form.priorityLevel,
          flowType: this.flowType,
          flowConditions: flowConditions,
          excludedFlowConditions: excludedFlowConditions,
          flowConfig: flowConfig.join(','),
        };
        this.spinning = true;
        if (this.type === 'add') {
          Add(request)
            .then((res) => {
              if (res.success) {
                this.$message.success(res.message);
                this.$emit('back');
              } else {
                this.$exception(res);
              }
            })
            .finally(() => {
              this.spinning = false;
            });
        } else {
          Update(request)
            .then((res) => {
              if (res.success) {
                this.$message.success(res.message);
                this.$emit('back');
              } else {
                this.$exception(res);
              }
            })
            .finally(() => {
              this.spinning = false;
            });
        }
        return;
      });
    },

    onFormChange(name, item, ...args) {
      if (!args[1]) {
        this.$set(item, 'unitId', '');
        this.$set(item, 'unitCode', '');
        this.$set(item, 'unitName', '');
        return;
      }
      if (name === 'consignor') {
        let idList = [],
          codeList = [];
        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 {
        let idList = [],
          codeList = [];
        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]);
      }
    },

    onAddBtn(listName) {
      this[listName].push({
        flowConditionType: '',
        flowConditionValue: '',
        unitId: '',
        unitCode: '',
        unitName: '',
      });
    },
    onDelBtn(index, listName) {
      this[listName].splice(index, 1);
      this.FLOW_CONDITION_TYPE_INIT(listName);
    },

    flowConditionTypeChange(value, item, listName) {
      if ([4, 5, 6, 7].includes(value)) {
        item.flowConditionValue = [];
      } else {
        item.flowConditionValue = '';
      }
      item.unitId = '';
      item.unitCode = '';
      item.unitName = '';
      this.FLOW_CONDITION_TYPE_INIT(listName);
    },
    flowConditionValueChange(value, item) {
      if (this.flowType === 2 && item.flowConditionType === 1 && value) {
        let flowType = value === 1 ? 'FLOW_CONFIG_ZY' : 'FLOW_CONFIG_ZT';
        this.processEnums = this[flowType].map((item) => {
          return { ...item };
        });
      }
    },

    // 进度条与多选框联动
    flowConfigChange(e) {
      this.processEnums.forEach((item) => {
        if (item.checkboxValue && item.checkboxValue === this.flowConfigEnums[item.value]) {
          item.groupcode1 = item.checked ? '1' : '2';
        }
      });
      // 自有、自提时联动
      if (this.flowType === 2 && this.flowConditions[0].flowConditionValue === 2 && this.flowConfigList.includes(e.target.value)) {
        let index1 = this.processEnums.findIndex((item) => this.flowConfigList.includes(item.checkboxValue) && item.checked);
        this.processEnums.forEach((item) => {
          if (item.checkboxValue === 'ifSelfPickUpVehReg') {
            item.checked = index1 > -1 ? true : false;
            item.groupcode1 = index1 > -1 ? '1' : '2';
          }
        });
      }
    },

    // 更新FLOW_CONDITION_TYPE
    FLOW_CONDITION_TYPE_INIT(listName) {
      let selectList = this[listName].map((item) => item.flowConditionType);
      this['FLOW_CONDITION_TYPE_' + listName].forEach((item) => {
        if (selectList.includes(item.value)) {
          item.disabled = true;
        } else {
          item.disabled = false;
        }
      });
    },

    excludedConditionsChange() {
      this.excludedFlowConditions = [];
    },
  },
};
</script>

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

.ant-select {
  width: 100%;
}

.process {
  display: flex;
//  overflow-x: auto;
  .processTitle {
    width: 110px;
    height: 40px;
    display: flex;
    align-items: center;
    font-weight: 700;
  }

  .processCheckbox {
    min-width: 110px;
    //height: 60px;
    padding: 5px 0;
    display: flex;
    align-items: center;
    margin-left: 0;
  }

  .processBox {
    width: 120px;
    height: 40px;
    line-height: 40px;
    padding-left: 5px;
    background: url('../img/processBox.png');
    background-size: auto 40px;
    background-repeat: no-repeat;
  }

  .processBoxChecked {
    background: url('../img/processBoxChecked.png');
    background-size: auto 40px;
    background-repeat: no-repeat;
  }
}
</style>