<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="picList">
        <multi-upload v-model="selectProductPics" :maxCount="1"></multi-upload>
      </el-form-item>
      <el-form-item label="技术领域" prop="field">
        <el-select v-model="ruleForm.field" placeholder="请选择">
          <el-option
            v-for="item in optionList1"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="成熟度" prop="maturityId">
        <el-select v-model="ruleForm.maturityId" placeholder="请选择">
          <el-option
            v-for="item in optionList2"
            :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="labelId">
        <el-select v-model="ruleForm.labelId" placeholder="请选择">
          <el-option
            v-for="item in optionList3"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </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 } from '@/api/technology/demand';
import { handleScienceResult, getInfo } from '@/api/technology/achievement';
import MultiUpload from '@/components/Upload/multiUpload';
export default {
  components: {
    Editor,
    MultiUpload
  },
  data() {
    // var valiIcon = (rule, value, callback) => {
    //   // 图片验证
    //   if (!this.selectProductPics[0]) {
    //     callback(new Error('请上传成果图片'));
    //   } else {
    //     callback();
    //   }
    // };
    return {
      editPage: false,
      ruleForm: {
        title: '',
        picList: '',
        field: '',
        maturityId: '',
        company: '',
        labelId: '',
        text: ''
      },
      optionList1: [],
      optionList2: [],
      optionList3: [],
      rules: {
        title: [{ required: true, message: '请输入', trigger: 'blur' }],
        field: [{ required: true, message: '请选择', trigger: 'change' }],
        maturityId: [{ required: true, message: '请选择', trigger: 'change' }],
        company: [{ required: true, message: '请填写', trigger: 'blur' }],
        labelId: [{ required: true, message: '请填写', trigger: 'blur' }],
        text: [{ required: true, message: '请填写内容', trigger: 'blur' }],
        picList: [{ required: true, message: '请上传', trigger: 'blur' }]
      }
    };
  },
  computed: {
    //主图和画册图片
    selectProductPics: {
      get: function() {
        let pics = [];
        if (
          this.ruleForm.picList === undefined ||
          this.ruleForm.picList == null ||
          this.ruleForm.picList === ''
        ) {
          return pics;
        }
        pics.push(this.ruleForm.picList);
        if (
          this.ruleForm.albumPics === undefined ||
          this.ruleForm.albumPics == null ||
          this.ruleForm.albumPics === ''
        ) {
          return pics;
        }
        let albumPics = this.ruleForm.albumPics.split(',');
        for (let i = 0; i < albumPics.length; i++) {
          pics.push(albumPics[i]);
        }
        return pics;
      },
      set: function(newValue) {
        if (newValue == null || newValue.length === 0) {
          this.ruleForm.picList = null;
          this.ruleForm.albumPics = null;
        } else {
          // this.ruleForm.picList = newValue[0];
          this.ruleForm.picList = newValue;
          this.ruleForm.albumPics = '';
          if (newValue.length > 1) {
            for (let i = 1; i < newValue.length; i++) {
              this.ruleForm.albumPics += newValue[i];
              if (i !== newValue.length - 1) {
                this.ruleForm.albumPics += ',';
              }
            }
          }
        }
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          console.log(this.ruleForm);
          // if (this.editPage) {
          //   // 修改
          //   updatePolicy(this.ruleForm).then(({ message }) => {
          //     this.msgSuccess(message);
          //   });
          // } else {
          //   // 添加
          //   addPolicy(this.ruleForm).then(({ message }) => {
          //     this.msgSuccess(message);
          //   });
          // }
          // this.$router.go(-1);
          handleScienceResult(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.optionList1 = data;
    });
    getDictListByStatus({ type: 3 }).then(({ data }) => {
      this.optionList2 = data;
    });
    getDictListByStatus({ type: 4 }).then(({ data }) => {
      this.optionList3 = data;
    });
    let { id } = this.$route.query;
    if (id) {
      this.editPage = true;
      getInfo({ id }).then(({ data }) => {
        this.ruleForm = data;
      });
    }
  }
};
</script>
