<template>
  <div class="app-container">
    <div>{{ editPage ? '修改' : '添加' }}需求</div>
    <el-form
      style="width:50%;margin:15px 0 0 15px"
      label-position="left"
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="80px"
    >
      <el-form-item label="需求标题" prop="title">
        <el-input placeholder="请输入" v-model="ruleForm.title"></el-input>
      </el-form-item>
      <el-form-item label="技术领域" prop="field">
        <el-select v-model="ruleForm.field" placeholder="请选择">
          <el-option
            v-for="item in optionList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="需求类型" prop="type">
        <el-select v-model="ruleForm.type" placeholder="请选择">
          <el-option
            v-for="item in typeList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="需求企业" prop="company">
        <el-input placeholder="请输入" v-model="ruleForm.company"></el-input>
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input placeholder="请输入" v-model="ruleForm.price"></el-input>
      </el-form-item>
      <el-form-item label="正文" prop="text">
        <editor v-model="ruleForm.text" :min-height="192" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确定</el-button
        >
        <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import Editor from '@/components/Editor';
import {
  getDictListByStatus,
  handleEnterpriseNeed,
  getInfo
} from '@/api/technology/demand';
export default {
  components: {
    Editor
  },
  data() {
    return {
      editPage: false,
      ruleForm: {
        title: '',
        field: '',
        type: '',
        company: '',
        price: '',
        text: ''
      },
      // 需求类型
      typeList: [],
      // 技术领域
      optionList: [],
      rules: {
        title: [{ required: true, message: '请输入', trigger: 'blur' }],
        field: [{ required: true, message: '请选择', trigger: 'change' }],
        type: [{ required: true, message: '请选择', trigger: 'change' }],
        company: [{ required: true, message: '请填写', trigger: 'blur' }],
        price: [{ required: true, message: '请填写', trigger: 'blur' }],
        text: [{ required: true, message: '请填写内容', trigger: 'blur' }]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // if (this.editPage) {
          //   // 修改
          //   updatePolicy(this.ruleForm).then(({ message }) => {
          //     this.msgSuccess(message);
          //   });
          // } else {
          //   // 添加
          //   addPolicy(this.ruleForm).then(({ message }) => {
          //     this.msgSuccess(message);
          //   });
          // }
          handleEnterpriseNeed(this.ruleForm).then(({ message }) => {
            this.msgSuccess(message);
            this.$router.go(-1);
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
    // resetForm(formName) {
    //   this.$refs[formName].resetFields();
    // }
  },
  created() {
    getDictListByStatus({ type: 1 }).then(({ data }) => {
      this.optionList = data;
    });
    getDictListByStatus({ type: 2 }).then(({ data }) => {
      this.typeList = data;
    });
    let { id } = this.$route.query;
    if (id) {
      this.editPage = true;
      getInfo({ id }).then(({ data }) => {
        this.ruleForm = data;
      });
    }
  }
};
</script>
