<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="roleModal-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 }">
          <v-select
            ref="orderStatus"
            id="addBoxModal-status"
            code="BOX_NUM"
            @change="handleNumChange"
            placeholder="请选择"
            allowClear
            v-decorator="[
              `formData.boxNum`,
              {
                initialValue: formData.boxNum,
                validateTrigger: ['change', 'blur'],
                rules: [
                  {
                    required: true, message: '请选择箱体类型!'
                  },
                ],
              },
            ]">
          </v-select>
        </a-form-item>
        <!-- 箱体限投 -->
        <a-row type="flex" align="middle" v-for="(k, index) in keys" :key="k">
          <a-col span="12">
            <!-- 投放开始时间 -->
            <a-form-item :label=" '内置'+(index+1)+'号箱限投(kg)'" :required="true" :label-col=" { span: 10 }" :wrapper-col=" { span: 14 }">
              <a-input-number
                :min="1"
                :max="1000"
                :precision="0"
                style="width: 200px;"
                placeholder="请输入数字(最大1000)"
                v-decorator="[
                  `formData.maxWeight[${k}]`,
                  {
                    initialValue: formData[`maxWeight[${k}]`],
                    validateTrigger: ['change', 'blur'],
                    rules: [
                      {
                        required: true, message: '请输入限投数量(最大1000)!'
                      },
                    ],
                  },
                ]" />
            </a-form-item>
          </a-col>
          <a-col span="1"></a-col>
          <a-col span="9">
            <!-- 请选择分类 -->
            <a-form-item :label="''" :required="false" :wrapper-col="{ span: 18 }">
              <v-select
                ref="orderStatus"
                id="addBoxModal-status"
                code="RUBBISH_TYPE"
                placeholder="请选择分类"
                allowClear
                v-decorator="[
                  `formData.rubbishType[${k}]`,
                  {
                    initialValue: formData[`rubbishType[${k}]`],
                    validateTrigger: ['change', 'blur'],
                    rules: [
                      {
                        required: true, message: '请选择分类!'
                      },
                    ],
                  },
                ]">
              </v-select>
            </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 {
  saveBoxSetting,
  viewBoxSetting
} from '@/api/boxSetting.js'
export default {
  name: 'RoleModal',
  components: {
    STable,
    VSelect
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    id: {
      type: [String, Number],
      default: function () {
        return ''
      }
    }
  },
  data () {
    return {
      titleText: '新增',
      isshow: this.visible,
      formLayout: 'horizontal',
      form: this.$form.createForm(this, {
        name: 'AddBoxModal'
      }),
      formData: {
        name: '',
        boxNum: '',
        maxWeight: [],
        rubbishType: []
      },
      keys: []
    }
  },
  methods: {
    cancel (e) {
      this.clear()
      this.$emit('close')
    },
    // 内置箱体选择修改
    handleNumChange (v) {
      console.log(v, 'vvvvvvvvvv')
      const arr = []
      for (let x = 0; x < v; x++) {
        arr.push(x)
      }
      this.keys = arr
      console.log(this.keys, 'this.keys')
    },
    // 查详情
    getDetailData (id) {
      viewBoxSetting({
        id: id
      }).then(res => {
        if (res.status == 200) {
          this.formData = Object.assign({}, this.formData, res.data)
		  this.handleNumChange(this.formData.boxNum)
          // 回显数据
          this.formData.deviceTypeBoxList.map((item, index) => {
            this.formData['maxWeight[' + index + ']'] = item.maxWeight
            this.formData['rubbishType[' + index + ']'] = item.rubbishType
          })
        }
      })
    },
    // 保存提交
    handleSubmit () {
      const _this = this
      this.form.validateFields((err, values) => {
        console.log(values, ' formData.type222222222')
        if (!err) {
          const params = Object.assign({}, this.formData, values.formData)
		  console.log(this.keys, params, 'kkkkkkkk')
		  const arr = []
          this.keys.map((item, index) => {
			  const p = {
				  boxCode: index + 1,
				  maxWeight: params.maxWeight[index],
				  rubbishType: params.rubbishType[index]
			  }
			 arr.push(p)
          })
		  delete params.maxWeight
		  delete params.rubbishType
		  params.deviceTypeBoxList = arr
		  console.log(params, 'ppppppppp')
          saveBoxSetting(params).then(res => {
            console.log(res, 'res--save')
            if (res.status == '200') {
              this.$message.success(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.boxNum = ''
    }

  },
  beforeCreate () {
    this.form = this.$form.createForm(this, {
      name: 'AddBoxModal'
    })
  },
  watch: {
    visible (newValue, oldValue) {
      this.isshow = newValue
    },
    isshow (newValue, oldValue) {
      if (newValue) {
        this.form.getFieldDecorator('keys', {
          initialValue: [],
          preserve: true
        })
        this.id = 0
        if (this.id) { // 编辑
          this.titleText = '编辑'
		  this.getDetailData(this.id)
        } 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>
