<template>
  <div>
    <a-modal
      class="modalBox"
      :footer="null"
      title="默认方案设置"
      v-model="isshow"
      @cancle="cancel"
      :width="600">
      <a-form :form="form" ref="form" @submit="handleSubmit">
        <a-row :gutter="24">
          <a-col :span="24">
            <!-- 视频巡店默认方案 -->
            <a-form-item label="视频巡店默认方案:" :label-col="{ span: 6 }" :wrapper-col="{ span: 16 }">
              <a-select
                id="setEvaDefaultModal-videoType"
                @change="videoTypeChange"
                v-decorator="[
                  'formData.videoType',
                  { initialValue: formData.videoType,
                    rules: [{ required: true, message: '请选择考评方案(单选)!' }] },
                ]"
                placeholder="请选择考评方案(单选)"
              >
                <a-select-option v-for="item in planList" :key="item.id" :value="item.id">
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="24">
            <!-- 现场巡店默认方案 -->
            <a-form-item label="现场巡店默认方案:" :label-col="{ span: 6 }" :wrapper-col="{ span: 16 }">
              <a-select
                id="setEvaDefaultModal-spotType"
                placeholder="请选择考评方案(单选)"
                @change="spotTypeChange"
                v-decorator="[
                  'formData.spotType',
                  { initialValue: formData.spotType,
                    rules: [{ required: true, message: '请选择考评方案(单选)!' }] },
                ]">
                <a-select-option v-for="item in planList" :key="item.id" :value="item.id">
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="22">
            说明：如果默认方案被删除，系统将自动获取对应类别下最新一条启用的模板作为默认方案模板
          </a-col>
        </a-row>
        <a-form-item :wrapper-col="{ span: 24, offset: 10 }">
          <a-button :loading="loading" id="setEvaDefaultModal-submit" type="primary" @click="handleSubmit()">
            确定
          </a-button>
          <a-button id="setEvaDefaultModal-cancle" :style="{ marginLeft: '8px' }" @click="cancel()">
            取消
          </a-button>
        </a-form-item>
      </a-form>

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

<script>
import {
  STable,
  VSelect
} from '@/components'
import {
  planQuery,
  planDefaultSave
} from '@/api/evaluationPlan.js'
export default {
  name: 'AddEvaModal',
  components: {
    STable,
    VSelect
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    // 要编辑的方案id
    itemId: {
      type: [String, Number],
      default: ''
    },
    // 默认方案
    defaultPlanList: {
      type: Array,
      default: function () {
        return []
      }
    }
  },
  watch: {
    visible (newValue, oldValue) {
      this.isshow = newValue
    },
    isshow (newValue, oldValue) {
      if (newValue) {
        this.form.resetFields()
        // 默认方案回显
        this.pageInit()
      } else {
        this.cancel()
      }
    }
  },
  data () {
    return {
      isshow: this.visible, // 弹框显示隐藏
      form: this.$form.createForm(this, {
        name: 'AddEvaModal'
      }),
	  // 默认值设为undefined, 解决placeholder不生效问题
      formData: {
        videoType: undefined, // 视频巡店默认方案
        spotType: undefined // 现场巡店默认方案
      },
      planList: [], // 考评方案列表
	  loading: false // 确定按钮loading
    }
  },
  computed: {},
  mounted () {
    // 获取方案列表数据
    this.getPlanList()
  },
  methods: {
    pageInit () {
      if (this.defaultPlanList.length) {
        this.formData.videoType = this.defaultPlanList.find(item => item.scopeType == 'VIDEO_INSPECTION').assessId
        this.formData.spotType = this.defaultPlanList.find(item => item.scopeType == 'SPOT_INSPECTION').assessId
      }
    },
    // 点击弹框x号触发事件
    cancel (e) {
      this.formData.videoType = undefined
      this.formData.spotType = undefined
      this.$emit('close')
    },
    // 获取方案列表数据
    getPlanList () {
      planQuery().then(res => {
        if (res.status == 200) {
          this.planList = res.data
        } else {
          this.planList = []
        }
      })
    },
    // 视频巡店默认方案改变
    videoTypeChange (v) {
      this.formData.videoType = v
    },
    // 现场巡店默认方案改变
    spotTypeChange (v) {
      this.formData.spotType = v
    },
    // 保存提交
    handleSubmit () {
      const _this = this
      this.form.validateFields((err, values) => {
        if (!err) {
          this.loading = true
          const params = [{
            assessId: values.formData.videoType,
            scopeType: 'VIDEO_INSPECTION'
          },
          {
            assessId: values.formData.spotType,
            scopeType: 'SPOT_INSPECTION'
          }
          ]
          planDefaultSave(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>
