<template>
  <p-modal
    :title="title"
    :visible="isvisible"
    @ok="handleOk"
    width="60%"
    :maskClosable="false"
    :destroyOnClose="true"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
  >
    <p-grid-form
      ref="forms"
      :span="24"
      :labelCol="{ span: 6 }"
      :wrapperCol="{ span: 18 }"
      :items="items"
      @submit="hanlderSubmit"
    />
  </p-modal>
</template>
<script>
import { remote } from '../../../basedata/warehouse/config/remote'
import { ruleMoveAdd, ruleMoveEedit } from '../api/api'
import API from '../api/default'
export default {
  data() {
    return {
      items: [
        { type: 'input', name: 'warehouseId', label: '仓库ID', hidden: true },
        {
          type: 'searchModal',
          name: 'warehouseName',
          label: '仓库',
          title: '仓库',
          targetValueKey: 'warehouseName',
          destroyOnClose: true,
          required: true,
          targetClick: true,
          value: '',
          toolBar: {
            items: [
              {
                type: 'input',
                name: 'warehouseName',
                label: '仓库名称',
                span: 8,
                value: '',
              },
              {
                type: 'input',
                name: 'warehouseCode',
                label: '仓库编码',
                span: 8,
                value: '',
              },
            ],
          },
          tableConfig: {
            rowKey: 'warehouseName',
            rowSelectType: 1,
            hasRowSelectCheck: true,
            columns: [
              { dataIndex: 'warehouseName', title: '仓库名称', key: 'warehouseName' },
              { dataIndex: 'warehouseCode', title: '仓库编码', key: 'warehouseCode' },
              { dataIndex: 'warehouseId', title: '仓库ID', key: 'warehouseId' },
            ],
          },
          value: '',
          remoteUrl: API.warehouse,
          on: {
            ok(event, value, rowData, form) {
              form.setFieldsValue({
                warehouseId: rowData[0].warehouseId,
              })
            },
            change(event, value, options, form) {
              if (!value) {
                form.setFieldsValue({ warehouseId: '' })
              }
            },
          },
        },
        {
          type: 'select',
          name: 'srcWarehouseCategory',
          label: '源库别',
          required: true,
          defaultValueIndex: 0,
          autoLoad: true,
          ...remote('wms_storeroom'),
        },
        {
          type: 'select',
          name: 'destWarehouseCategory',
          label: '目的库别',
          required: true,
          defaultValueIndex: 0,
          autoLoad: true,
          ...remote('wms_storeroom'),
        },
        {
          type: 'input',
          name: 'priority',
          label: '执行顺序',
          rules: [
            {
              pattern: '^[0-9][0-9]?$',
              required: true,
            },
          ], //自定义验证规则
        },
        // {
        //   type: 'p-checkbox-group',
        //   name: 'isCheck',
        //   label: '库存状态',
        //   required: true,
        //   value: [],
        //   options: []
        // },
        {
          type: 'radio',
          name: 'isCheck',
          label: '库存状态',
          required: true,
          value: '',
          options: [],
        },
        {
          type: 'radio',
          name: 'quantityMode',
          label: '包装属性',
          required: true,
          value: '',
          options: [
            { label: '整件', value: '1' },
            { label: '零散', value: '2' },
            { label: '整散', value: '3' },
          ],
        },
        {
          type: 'radio',
          name: 'ruleType',
          hidden: true,
          label: '规则类型',
          value: '',
          options: [],
        },
        {
          type: 'switch',
          name: 'isFlage',
          label: '是否启用',
          required: true,
          value: true,
        },
        {
          type: 'switch',
          name: 'auditFlag',
          label: '是否需要审核',
          required: true,
          value: true,
        },
        {
          type: 'switch',
          name: 'syncFlag',
          label: '是否回传ERP',
          required: true,
          value: true,
        },
      ],
      isvisible: false,
      confirmLoading: false,
    }
  },
  components: {},
  props: {
    visibleDetial: {
      type: Boolean,
      default: false,
    },
    title: {
      type: String,
      default: '',
    },
    setRecord: {
      type: Object,
      default: {},
    },
    optionsData: {
      type: Array,
      default: [],
    },
    typeRule: {
      type: Array,
      default: [],
    },
    businessData: {
      type: String,
      default: '',
    },
  },
  watch: {
    visibleDetial(newVal) {
      this.items[5].options = this.optionsData
      this.isvisible = newVal
      if (this.setRecord.moveRuleId) {
        // 判断修改
        this.$nextTick(() => {
          this.$refs.forms.form.setFieldsValue({
            srcWarehouseCategory: this.setRecord.srcWarehouseCategory,
            destWarehouseCategory: this.setRecord.destWarehouseCategory,
            priority: String(this.setRecord.priority),
            // isCheck: this.setRecord.stockStatus.split(','),
            isCheck: this.setRecord.stockStatus,
            ruleType: this.setRecord.ruleType,
            quantityMode: String(this.setRecord.quantityMode),
            warehouseId: this.setRecord.warehouseId,
            warehouseName: this.setRecord.warehouseName,
            isFlage: this.setRecord.isEnabled == '1' ? true : false,
            auditFlag: this.setRecord.auditFlag == '1' ? true : false,
            syncFlag: this.setRecord.syncFlag == '1' ? true : false,
          })
        })
      }
    },
  },
  created() {},
  methods: {
    callback(key) {},
    handleCancel() {
      // 取消
      this.isvisible = false
      this.$emit('cancelRule', false)
    },
    selectChange(data) {
      // 勾选框
      // this.
      this.checkData = data
    },
    handleOk() {
      // 确认
      this.$refs.forms.form.validateFields((error, values) => {
        if (!error) {

            if (values.srcWarehouseCategory == values.destWarehouseCategory && values.srcWarehouseCategory == 'LK') {
                this.$message.warn('立库与立库之间不能执行移库操作')
                return
            }
            if (values.destWarehouseCategory == 'LK') {
                this.$message.warn('移库操作不能移入立库')
                return
            }


          if (this.setRecord.moveRuleId) {
            //修改
            // values.stockStatus = values.isCheck.join(',')
            values.stockStatus = values.isCheck
            values.inventoryStatusUnchanged = values.isCheck
            values.isEnabled = values.isFlage ? '1' : '0'
            values.auditFlag = values.auditFlag ? '1' : '0'
            values.syncFlag = values.syncFlag ? '1' : '0'
            values.moveRuleId = this.setRecord.moveRuleId
            this.confirmLoading = true
            this.changeData(values)
          } else {
            // 新增
            // values.stockStatus = values.isCheck.join(',')
            values.stockStatus = values.isCheck
            values.inventoryStatusUnchanged = values.isCheck
            values.isEnabled = values.isFlage ? '1' : '0'
            values.auditFlag = values.auditFlag ? '1' : '0'
            values.syncFlag = values.syncFlag ? '1' : '0'
            this.confirmLoading = true
            this.saveData(values)
          }
        }
      })
    },
    saveData(values) {
      // 保存
      ruleMoveAdd(values).then((res) => {
        this.confirmLoading = false
        if (res.success) {
          this.$message.success('成功添加规则！')
          this.$emit('requestSuccess', true)
        } else {
          this.$exception(res)
        }
      })
    },
    changeData(values) {
      // 修改
      ruleMoveEedit(values).then((res) => {
        this.confirmLoading = false
        if (res.success) {
          this.$message.success('成功修改规则！')
          this.$emit('requestSuccess', true)
        } else {
          this.$exception(res)
        }
      })
    },
    hanlderSubmit(key) {
      // 查询
      // event.preventDefault()
      // this.$emit('hanlderSubmit', event, form)
      // this.$refs[key][0].form.validateFields((error, values) => {
      //   if (!error) {
      //     this.remoteParams = { ...this.tableRemoteParams }
      //     this.remoteParams.params = {
      //       ...values
      //     }
      //   }
      // })
    },
  },
}
</script>
