<template>
  <el-dialog
    v-loading="loading"
    :title="title"
    :center="true"
    align="left"
    top="18vh"
    width="500px"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    @close="showModelThis = false"
  >
    <el-card style="margin: -20px 0 0 0">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            status-icon
            :rules="rules"
            label-width="80px"
            class="demo-ruleForm"
          >
            <el-form-item label="行业名称" prop="industryName">
              <el-input
                v-model="ruleForm['industryName']"
                :disabled="id == '007'"
                placeholder="行业名称"
                maxlength="20"
              />
            </el-form-item>

            <el-form-item label="行业资质" prop="industryQualification">
              <el-select
                style="width:100%"
                v-model="ruleForm['industryQualification']"
                :disabled="id == '007'"
                placeholder="行业资质"
              >
                <el-option label="需要" value="1" />
                <el-option label="不需要" value="0" />
              </el-select>
            </el-form-item>
            <el-form-item label="关联需求项目" prop="relaDemandProject" label-width="110px">
              <el-checkbox-group
                style="width:100%"
                v-model="ruleForm['relaDemandProject']"
                :disabled="id == '007'"
                placeholder="关联需求项目"
              >
                <el-checkbox />
              </el-checkbox-group>
            </el-form-item>

            <el-form-item label="关联行业标签" prop="relaIndustryLabel" label-width="110px">
              <el-checkbox-group
                style="width:100%"
                v-model="ruleForm['relaIndustryLabel']"
                :disabled="id == '007'"
                placeholder="关联行业标签"
              >
                <el-checkbox />
              </el-checkbox-group>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="24" style="text-align: right">
          <el-button
            v-if="id !== '007'"
            type="primary"
            @click="submitForm"
          >保 存
          </el-button>
          <el-button @click="showModelThis = false">{{
            id == "007" ? "关 闭" : "取 消"
          }}</el-button>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
<script>
import asyncRequest from '@/apis/service/serviceParam/industryManagement'
import resToken from '@/mixins/resToken'

/**
 * @props
 * industryName // 行业名称
 * industryQualification // 行业资质
 * relaDemandProject //  关联需求项目
 * relaIndustryLabel // 关联行业标签
*/
export default {
  name: 'Brand',
  mixins: [resToken],
  props: ['showModel', 'id', 'sitem'],
  data() {
    return {
      loading: false,
      title: '添加行业',
      showModelThis: this.showModel,
      select: '1',
      activeOptions: [],
      actionList: [],
      ruleForm: {
        industryName:'',
        industryQualification:'',
        relaDemandProject:'',
        relaIndustryLabel:'',
      },
      rules: {
        industryName:[
          {
            required:true,
            trigger:'blur',
            message:'请输入行业名称'
          }
        ],
        industryQualification:[
          {
            required:true,
            trigger:'blur',
            message:'请输入行业资质'
          }
        ],
        relaDemandProject:[
          {
            required:true,
            trigger:'blur',
            message:'请选择关联需求项目'
          }
        ],
        relaIndustryLabel:[
          {
            required:true,
            trigger:'blur',
            message:'请选择关联行业标签属性'
          }
        ]
      }
    }
  },
  watch: {
    showModel: function(val) {
      this.showModelThis = val
      if (val) {
        this.initForm()
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit('cancel')
      }
    }
  },
  methods: {
    async initForm() {
      this.loading = true
      const basicTitle = '行业'
      let prefix = this.id === '005' ? '修改' : this.id === '003' ? '添加' : ''
      this.title = prefix ? prefix + basicTitle : basicTitle + '详情'
      await this.resetForm()
      this.loading = false
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields()
          this.$refs.ruleForm.clearValidate()
          const {         
          industryName,
          industryQualification,
          relaDemandProject,
          relaIndustryLabel 
        } = this.sitem
          this.ruleForm = {
            industryName: industryName || '',
            industryQualification:industryQualification || '',
            relaDemandProject:relaDemandProject || '',
            relaIndustryLabel:relaIndustryLabel || '',
          }
        }
      })
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async(valid) => {
        if (valid) {
          if (!this.loading) {
            this.loading = true
            const model = JSON.parse(JSON.stringify(this.ruleForm))
            let res = {}
            if (this.id === '003') {
              delete model['id']
              res = await asyncRequest.add(model)
            } else {
              res = await asyncRequest.update(model)
            }
            this.loading = false
            if (res && res.code === 0) {
              const title = this.id === '003' ? '添加成功!' : '修改成功!'
              this.$notify.success({
                title,
                message: ''
              })
              this.showModelThis = false
              // 刷新
              this.$emit('refresh')
            } else if (res && res.code >= 100 && res.code <= 104) {
              await this.logout()
            } else {
              this.$message.warning(res.message)
            }
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.brand {
}
</style>
