<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 { genWarehouseAuthSearchModal, genCargoownerAuthSearchModal } from '../../../SearchModal'
import API from '../api/default'
export default {
  data() {
    return {
      items: [
        ...genWarehouseAuthSearchModal(1, true, false, null, null, {
          labelCol: { span: 6 },
          span: 24,
        }),
        ...genCargoownerAuthSearchModal(1, true, false, null, null, {
          labelCol: { span: 6 },
          span: 24,
        }),
        {
          type: 'select',
          name: 'wmsStatus',
          label: 'WMS库存状态',
          required: true,
          autoLoad: true,
          ...remote('wms_invstatus'),
        },
        {
          type: 'select',
          name: 'erpStatus',
          label: 'ERP库存状态(可多选)',
          required: true,
          mode: 'multiple',
          autoLoad: true,
          ...remote('	erp_invstatus'),
        },
        {
          type: 'switch',
          name: 'isEnabled',
          label: '是否启用',
          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: [],
    },
  },
  watch: {
    visibleDetial(newVal) {
      this.items[5].options = this.optionsData
      this.isvisible = newVal
      if (this.setRecord.invAdjustId) {
        // 判断修改
        this.$nextTick(() => {
          this.$refs.forms.form.setFieldsValue({
            erpStatus: this.setRecord.erpStatus,
            wmsStatus: this.setRecord.wmsStatus,
            warehouseId: this.setRecord.warehouseId,
            warehouseName: this.setRecord.warehouseName,
            cargoOwnerId: this.setRecord.cargoOwnerId,
            cargoOwnerName: this.setRecord.cargoOwnerName,
            isEnabled: this.setRecord.isEnabled == '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 (Array.isArray(values.erpStatus)) {
            values.erpStatus = values.erpStatus.join(',')
          }
          if (this.setRecord.invAdjustId) {
            //修改
            // values.stockStatus = values.isCheck.join(',')
            values.invAdjustId = this.setRecord.invAdjustId
            values.isEnabled = values.isEnabled ? '1' : '0'
            this.confirmLoading = true
            this.changeData(values)
          } else {
            // 新增
            // values.stockStatus = values.isCheck.join(',')
            values.isEnabled = values.isEnabled ? '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>
