<template>
  <div>
    <a-modal
      class="modalBox"
      :footer="null"
      :title="titleText"
      v-model="isshow"
      @cancle="cancel"
      :width="600">
      <a-form :form="form" :model="formData" ref="form" @submit="handleSubmit">
        <a-row :gutter="24">
          <a-col :span="20">
            <!-- 考评方案名称 -->
            <a-form-item label="考评方案名称:" :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }">
              <a-input
                allowClear
                id="addEvaModal-name"
                :maxLength="128"
                v-decorator="[
                  'formData.name',
                  { initialValue: formData.name,
                    getValueFromEvent: $filterEmpty,
                    rules: [{ required: true, message: '请输入考评方案名称!' }] },
                ]"
                placeholder="请输入考评方案名称(最多128个字符)" />
            </a-form-item>
          </a-col>
        </a-row>

        <a-row :gutter="24">
          <a-col :span="20">
            <!-- 适用模式 -->
            <a-form-item label="适用模式:" :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }">
              <v-select
                ref="orgType"
                id="addEvaModal-scopeType"
                allowClear
                mode="multiple"
                @change="scopeTypeChange"
                v-decorator="[
                  'formData.scopeType',
                  { initialValue: formData.scopeType,
                    rules: [{ required: true, message: '请选择适用模式!' }] },
                ]"
                code="TASK_TYPE"></v-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="20">
            <!-- 考评方案说明 -->
            <a-form-item label="考评方案说明:" :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }">
              <a-textarea
                allowClear
                id="addEvaModal-desc"
                :maxLength="500"
                Input.TextArea
                :autoSize="{minRows:5}"
                v-decorator="[
                  'formData.desc',
                  { initialValue: formData.desc,getValueFromEvent: $filterEmpty },
                ]"
                placeholder="请输入考评方案说明(最多500个字符)" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-form-item :wrapper-col="{ span: 24, offset: 10 }">
          <a-button :loading="loading" id="addEvaModal-submit" type="primary" @click="handleSubmit()">
            保存
          </a-button>
          <a-button id="addEvaModal-cancel" :style="{ marginLeft: '8px' }" @click="cancel()">
            取消
          </a-button>
        </a-form-item>
      </a-form>

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

<script>
import {
  VSelect
} from '@/components'
import {
  planSave,
  planDetail
} from '@/api/evaluationPlan.js'
export default {
  name: 'AddEvaModal',
  components: {
    VSelect
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    // 要编辑的方案id
    itemId: {
      type: [String, Number],
      default: ''
    }
  },
  watch: {
    visible (newValue, oldValue) {
      this.isshow = newValue
    },
    isshow (newValue, oldValue) {
      if (newValue) {
        this.form.resetFields()
        // 编辑查详情
        if (this.itemId) {
          this.getFormData(this.itemId)
        }
      } else {
        this.cancel()
      }
    }
  },
  data () {
    return {
      isshow: this.visible, // 弹框显示隐藏
      form: this.$form.createForm(this, {
        name: 'AddEvaModal'
      }),
      formData: {
        name: '', // 方案名称
        scopeType: [], // 适用类型
        desc: '' // 方案说明
      },
      loading: false, // 确定按钮loading
      options: [{
        code: 'VIDEO_INSPECTION',
        dispName: '视频巡店',
        id: 1
      },
      {
        code: 'SPOT_INSPECTION',
        dispName: '现场巡店',
        id: 12
      },
      {
        code: 'POINT_INSPECTION',
        dispName: '点检',
        id: 13
      }
      ]
    }
  },
  computed: {
    // 弹框标题
    titleText () {
      return this.itemId ? '编辑' : '新增'
    }
  },
  methods: {
    // 点击弹框x号触发事件
    cancel (e) {
      this.formData = {
        name: '', // 方案名称
        scopeType: [], // 适用类型
        desc: '' // 方案说明
      }
      this.$emit('close')
    },
    // 查详情
    getFormData (id) {
      planDetail({
        id: id
      }).then(res => {
        if (res.status == 200) {
          // console.log(res, 'rrrrrrrrrr')
          this.formData = Object.assign(this.formData, res.data)
          this.formData.scopeType = this.formData.scopeType.split(',')
        }
      })
    },
    // 使用模式改变
    scopeTypeChange (v) {
      this.formData.scopeType = v
    },
    // 保存提交
    handleSubmit () {
      const _this = this
      this.form.validateFields((err, values) => {
        if (!err) {
          this.loading = true
          const params = this.itemId ? Object.assign({
            id: this.itemId
          }, values.formData) : values.formData
          params.scopeType = params.scopeType.join(',')
          // console.log(params, 'ppppppppppppp')
          planSave(params).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.warning(res.message)
            }
            this.loading = false
          })
        }
      })
    },
    // 取消
    handleCancel () {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '确定取消吗?',
        okText: '确定',
        cancelText: '取消',
        onOk () {
          _this.cancel()
        }
      })
    }
  },
  beforeCreate () {
    this.form = this.$form.createForm(this, {
      name: 'AddEvaModal'
    })
  }

}
</script>

<style scoped>
	.modalBox {}

	.ant-modal-footer {
		display: none;
	}

	.time-text {
		color: #1890FF;
		padding: 0px 20px;
		cursor: pointer;
	}

	.labelT {
		position: absolute;
		left: -135px;
		top: 20px;
		color: rgba(0, 0, 0, 0.85);
	}
</style>
