<template>
  <a-modal
    :title="title"
    width="60%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input
            placeholder="请输入名称"
            v-decorator="[
              'name',
              {
                validateFirst: true,
                rules: [
                  { required: true, message: '请输入名称' },
                  { max: 20, message: '名称最多20个字' }
                ]
              }
            ]"
          />
        </a-form-item>
        <a-form-item label="排序号" help="数字越小越靠前" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input placeholder="请输入排序号" v-decorator="['sort']" />
        </a-form-item>
        <a-form-item label="价格" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input
            type="number"
            placeholder="请输入价格"
            v-decorator="[
              'price',
              {
                validateFirst: true,
                rules: [
                  { required: true, message: '请输入价格' },
                  {
                    validator: (rule, value, callback) => {
                      if (value < 0) {
                        callback('价格不可小于0元')
                      } else {
                        callback()
                      }
                    }
                  }
                ]
              }
            ]"
            suffix="元"
          />
        </a-form-item>
        <a-form-item label="周期" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input
            type="number"
            placeholder="请输入周期值"
            v-decorator="[
              'expirevalue',
              {
                validateFirst: true,
                rules: [
                  { required: true, message: '请输入周期值' },
                  {
                    validator: (rule, value, callback) => {
                      if (value < 0) {
                        callback('周期值不可小于0')
                      } else {
                        callback()
                      }
                    }
                  }
                ]
              }
            ]"
          >
            <a-select slot="addonAfter" v-decorator="['etype']" style="width: 80px">
              <a-select-option
                v-for="(item, index) in GetClientDataList('vipexpiretype')"
                :key="index"
                :value="item.code"
                >{{ item.val }}</a-select-option
              >
            </a-select>
          </a-input>
        </a-form-item>
        <a-form-item
          label="产品数"
          help="能够添加的最大产品数量,0表示无限制"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            type="number"
            placeholder="请输入产品数"
            v-decorator="[
              'productcnt',
              {
                validateFirst: true,
                rules: [
                  { required: true, message: '请输入产品数' },
                  {
                    validator: (rule, value, callback) => {
                      if (value < 0) {
                        callback('数量不可小于0')
                      } else {
                        callback()
                      }
                    }
                  }
                ]
              }
            ]"
          />
        </a-form-item>
        <a-form-item
          label="供货数"
          help="能够添加的最大供货数量,0表示无限制"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            type="number"
            placeholder="请输入供货数"
            v-decorator="[
              'supplycnt',
              {
                validateFirst: true,
                rules: [
                  { required: true, message: '请输入供货数' },
                  {
                    validator: (rule, value, callback) => {
                      if (value < 0) {
                        callback('数量不可小于0')
                      } else {
                        callback()
                      }
                    }
                  }
                ]
              }
            ]"
          />
        </a-form-item>
        <a-form-item
          label="采购数"
          help="能够添加的最大采购数量,0表示无限制"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            type="number"
            placeholder="请输入采购数"
            v-decorator="[
              'demandcnt',
              {
                validateFirst: true,
                rules: [
                  { required: true, message: '请输入采购数' },
                  {
                    validator: (rule, value, callback) => {
                      if (value < 0) {
                        callback('数量不可小于0')
                      } else {
                        callback()
                      }
                    }
                  }
                ]
              }
            ]"
          />
        </a-form-item>
        <a-form-item label="关联权限" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            placeholder="请选择关联权限"
            mode="multiple"
            v-decorator="['auths', { rules: [], initialValue: selectedauthlist }]"
          >
            <a-select-option v-for="(item, index) in authlist" :key="index" :value="item.id">{{
              item.name
            }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea
            :rows="4"
            placeholder="请输入备注"
            v-decorator="[
              'remark',
              {
                validateFirst: true,
                rules: [{ max: 200, message: '备注最多200个字' }]
              }
            ]"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { GetClientDataList } from '@/utils/clientdata'
import { Submit, VipAuthList } from '@/api/modular/main/vip'
export default {
  props: {
    title: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 }
      },
      currentInfo: {},
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
      authlist: [],
      selectedauthlist: []
    }
  },
  methods: {
    GetClientDataList,
    // 初始化方法
    init(record) {
      this.visible = true
      this.currentInfo = record
      if (record && record.id) {
        this.type = record.ptype
        this.headimg = record.headimg
        this.imageUrl = record.imageurl
        setTimeout(() => {
          this.form.setFieldsValue({
            name: record.name,
            sort: record.sort,
            price: record.price,
            remark: record.remark,
            expirevalue: record.expirevalue,
            etype: record.etype,
            productcnt: record.productcnt,
            supplycnt: record.supplycnt,
            demandcnt: record.demandcnt
          })
        }, 100)
      }
      this.AuthList()
    },
    AuthList() {
      VipAuthList({
        id: this.currentInfo.id ? this.currentInfo.id : null
      }).then(res => {
        this.authlist = res.data
        this.selectedauthlist = this.authlist
          .filter(item => {
            return item.ischecked
          })
          .map(item => {
            return item.id
          })
      })
    },
    handleSubmit() {
      const {
        form: { validateFields }
      } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          values['id'] = this.currentInfo.id ? this.currentInfo.id : null
          Submit(values)
            .then(res => {
              if (res.code === 0) {
                if (this.currentInfo && this.currentInfo.id) {
                  this.$message.success('编辑成功')
                } else {
                  this.$message.success('添加成功')
                }
                this.confirmLoading = false
                this.$emit('ok', this.record)
                this.handleCancel()
              } else {
                this.$myerror(res.message)
              }
            })
            .finally(res => {
              this.confirmLoading = false
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.form.resetFields()
      this.visible = false
    }
  }
}
</script>
