<template>
  <div>
    <a-modal
      class="modalBox"
      :title="titleText"
      v-model="isshow"
      :footer="null"
      @cancle="cancel"
      :width="800"
      :centered="true">
      <a-form :form="form" @submit="handleSubmit">
        <!-- 规则名称 -->
        <a-form-item label="规则名称" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
          <a-input
            placeholder="请输入规则名称(最多30个字符)"
            allowClear
            :maxLength="30"
            id="addSetModal-name"
            v-decorator="['formData.name', {
              initialValue: formData.name,getValueFromEvent: $filterEmpty,
              rules: [{ required: true, message: '请输入规则名称(最多30个字符)!' }] }]" />
        </a-form-item>
        <!-- 规则说明 -->
        <a-form-item label="规则说明" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
          <a-input
            placeholder="请输入规则说明(最多500个字符)"
            allowClear
            :autoSize="{minRows: 3}"
            type="textarea"
            :maxLength="500"
            id="addSetModal-remarks"
            v-decorator="['formData.remarks', {
              initialValue: formData.remarks,getValueFromEvent: $filterEmpty,
              rules: [{ required: true, message: '请输入规则说明(最多500个字符)!' }] }]" />
        </a-form-item>
        <!-- 箱体限投 -->
        <a-row
          type="flex"
          align="middle"
          v-for="(item, index) in keys"
          :key="index">
          <a-col span="11">
            <!-- 请输入投放重量 -->
            <a-form-item
              :label="item.dispName"
              :required="true"
              :label-col=" { span: 11 }"
              :wrapper-col=" { span: 13 }"
            >
              <a-input-number
                :min="1"
                :max="999999"
                :precision="2"
                :disabled="!item.exchangeStatus"
                style="width: 150px;"
                placeholder="请输入投放重量(最大999999)"
                v-decorator="[
                  `formData.settings[${index}].weight`,
                  {
                    validateTrigger: ['change', 'blur'],
                    rules: [
                      {
                        required: true, message: '请输入投放重量(最大999999)!'
                      },
                    ],
                  },
                ]"
              />
              <span>（g）</span>
            </a-form-item>
          </a-col>
          <a-col span="1"></a-col>
          <a-col span="8">
            <!-- 请输入可兑乐豆数 -->
            <a-form-item
              :label="''"
              :required="false"
              :wrapper-col="{ span: 18 }">
              <a-input-number
                :min="1"
                :max="999999"
                :precision="0"
                :disabled="!item.exchangeStatus"
                style="width: 140px;"
                placeholder="请输入可兑乐豆数(最大999999)"
                v-decorator="[
                  `formData.settings[${index}].num`,
                  {
                    validateTrigger: ['change', 'blur'],
                    rules: [
                      {
                        required: true, message: '请输入可兑乐豆数(最大999999)!'
                      },
                    ],
                  },
                ]"
              />
              <span>（个）</span>
            </a-form-item>
          </a-col>
          <a-col span="3">
            <a-form-item
              :label="''"
              :required="false"
              :wrapper-col="{ span: 24 }">
              <a-checkbox :checked="!item.exchangeStatus" @change="handleStatusChange(item,index)">
                不兑换
              </a-checkbox>
            </a-form-item>
          </a-col>
        </a-row>
        <a-form-item :wrapper-col="{ span: 24, offset: 10 }">
          <a-button type="primary" @click="handleSubmit()" id="roleModal-handleSubmit" :style="{ marginRight: '15px' }">
            保存
          </a-button>
          <a-button :style="{ marginLeft: '15px' }" @click="handleCancel()" id="roleModal-handleCancel">
            取消
          </a-button>
        </a-form-item>
      </a-form>

    </a-modal>
  </div>
</template>

<script>
import {
  STable,
  VSelect
} from '@/components'
import { viewExchangeSetting, saveExchangeSetting } from '@/api/exchangeSetting.js'
import { listLookUp } from '@/api/data'
export default {
  name: 'RoleModal',
  components: {
    STable,
    VSelect
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  data () {
    return {
      id: 0,
      titleText: '新增',
      isshow: this.visible,
      formLayout: 'horizontal',
      form: this.$form.createForm(this, {
        name: 'AddBoxModal'
      }),
      formData: {
        name: '',
        remarks: '',
        settings: []
      },
      keys: []
    }
  },
  methods: {
    cancel (e) {
      this.clear()
      this.$emit('close')
    },
    // 查详情
    getDetailData (id) {
	  viewExchangeSetting({ id: id }).then(res => {
	    if (res.status == 200) {

	    }
	  })
    },
    // 是否兑换切换
    handleStatusChange (item, index) {
      item.exchangeStatus = item.exchangeStatus ? 0 : 1
	  this.form.setFieldsValue({ '`formData.settings[${index}].num`': '' })
	  // this.formData.settings[index].num = ''
	  // this.formData.settings[index].weight = ''
    },
    // 保存提交
    handleSubmit () {
      const _this = this
      this.form.validateFields((err, values) => {
		  console.log(values, ' formData.type222222222')
        if (!err) {
          saveExchangeSetting(Object.assign(this.formData, values.formData)).then(res => {
            console.log(res, 'res--save')
            if (res.status + '' === '200') {
              this.$message.success(res.message ? res.message : '保存成功')
              this.$emit('refresh')
              setTimeout(function () {
                _this.cancel()
              }, 300)
            } else {
              // this.$message.error(res.message)
            }
          })
        }
      })
    },
    // 取消
    handleCancel () {
      this.cancel()
    },
    clear () {
      this.form.resetFields()
      this.formData.name = ''
      this.formData.isEnable = ''
      this.formData.remarks = ''
    }

  },
  mounted () {},
  beforeCreate () {
    this.form = this.$form.createForm(this, {
      name: 'AddBoxModal'
    })
  },
  watch: {
    visible (newValue, oldValue) {
      this.isshow = newValue
    },
    isshow (newValue, oldValue) {
      if (newValue) {
		  this.keys = [
			  { dispName: '废旧衣物', code: 'fjyw', id: 11, exchangeStatus: 1 },
			  { dispName: '废旧衣物', code: 'fjyw', id: 12, exchangeStatus: 1 },
			  { dispName: '废旧衣物', code: 'fjyw', id: 13, exchangeStatus: 1 },
			  { dispName: '废旧衣物', code: 'fjyw', id: 14, exchangeStatus: 1 },
			  { dispName: '废旧衣物', code: 'fjyw', id: 15, exchangeStatus: 0 }
		  ]
        if (this.data.id) { // 编辑
          this.titleText = '编辑'
          this.formData = Object.assign({}, this.data)
          delete this.formData.no
          this.formData.isEnable = Number(this.formData.isEnable)
          console.log(this.formData, 'this.formData')
        } else {
          this.titleText = '新增'
        }
      } else {
        this.cancel()
      }
    }
  }
}
</script>

<style scoped>
	.modalBox {}

	.ant-modal-footer {
		display: none;
	}
	.dynamic-delete-button {
		margin-right: 20px;
	}
	.linenums{
		padding: 0 20px;
	}
</style>
