<template>
  <div id="release">
    <el-row>
      <el-col :span="24" class="info">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="产品类型" prop="totaltype">
            <el-select v-model="form.totaltype" placeholder="请选择产品类型">
              <el-option v-for="(item, index) in totaltype_list" :key="index" :value="item.value" :label="item.name"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="产品名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入产品名称"></el-input>
          </el-form-item>
          <el-form-item label="类型">
            <el-select v-model="form.product_type_id" filterable @change="selectChild">
              <el-option v-for="(item, index) in columnList" :key="index" :value="item.id" :label="item.name"></el-option>
            </el-select>
          </el-form-item>
          <span v-if="form.totaltype == 0">
            <el-form-item label="研发阶段">
              <el-select v-model="form.phase" placeholder="请选择研发阶段">
                <el-option label="阶段成果" value="1"></el-option>
                <el-option label="最终成果" value="2"></el-option>
              </el-select>
            </el-form-item>
          </span>
          <el-form-item label="产品简介">
            <el-input v-model="form.introduction" placeholder="请输入产品简介"></el-input>
          </el-form-item>
          <el-row>
            <el-col :span="12">
              <el-form-item label="产品单价">
                <el-input v-model="form.price" placeholder="产品单价"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="产品单位">
                <el-select v-model="form.priceunit" placeholder="单位">
                  <el-option v-for="(item, index) in priceunit_list" :key="index" :label="item.name" :value="item.name"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="产品图片" prop="image">
            <upload
              :limit="1"
              :data="form.image"
              :uploadBtn="true"
              type="image"
              :url="`/files/image/upload`"
              @upload="uploadSuccess"
              @delete="uploadDelete"
            ></upload>
          </el-form-item>

          <span v-if="form.totaltype == 0 || form.totaltype == 2">
            <el-form-item label="应用领域">
              <el-input v-model="form.field" placeholder="请输入应用领域"></el-input>
            </el-form-item>
          </span>
          <el-form-item label="服务范围">
            <el-input type="textarea" v-model="form.scope" placeholder="请输入服务范围"></el-input>
          </el-form-item>
          <el-form-item label="交易方式">
            <el-select v-model="form.business" placeholder="请选择交易方式">
              <el-option label="公用" value="0"></el-option>
              <el-option label="竞价" value="1"></el-option>
              <el-option label="转让" value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="联系人" prop="contact_user">
            <el-input v-model="form.contact_user" placeholder="请输入联系人"></el-input>
          </el-form-item>
          <el-form-item label="联系电话" prop="contact_tel">
            <el-input v-model="form.contact_tel" placeholder="请输入联系电话"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit(form)">发布</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import upload from '@/components/upload.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: maarkettype } = createNamespacedHelpers('maarkettype');
const { mapActions: marketproduct } = createNamespacedHelpers('marketproduct');

export default {
  name: 'release',
  props: {},
  components: {
    upload,
  },
  data: function() {
    return {
      form: {},
      // 产品类型
      totaltype_list: [
        { name: '技术', value: '0' },
        { name: '产品', value: '1' },
        { name: '服务', value: '2' },
      ],
      // 类型
      columnList: [],
      // 单位
      priceunit_list: [
        {
          name: '公斤',
        },
        {
          name: '套',
        },
        {
          name: '件',
        },
      ],
      rules: {
        totaltype: [{ required: true, message: '请选择产品类型', trigger: 'blur' }],
        name: [{ required: true, message: '请输入产品名称', trigger: 'blur' }],
      },
    };
  },
  created() {
    this.searchType();
  },
  methods: {
    ...maarkettype(['query']),
    ...marketproduct({ productFetch: 'fetch', productCreate: 'create', productUpdate: 'update', productQuery: 'query' }),

    // 提交发布
    async onSubmit(form) {
      form.status = '0';
      form.userid = this.user.uid;
      let res;
      let msg;
      res = await this.productCreate(form);
      msg = `${this.keyWord}添加成功`;
      this.$checkRes(res, '添加成功', '添加失败');
      this.$router.push({ path: '/userCenter/myProduct/index' });
    },

    // 查询类型
    async searchType({ category = 54, ...info } = {}) {
      const res = await this.query({ category, ...info });
      if (this.$checkRes(res)) {
        this.$set(this, `columnList`, res.data);
      }
    },
    // 类型选择
    selectChild(product_type_id) {
      let res = this.columnList.filter(fil => fil.id === product_type_id);
      if (res.length > 0) {
        this.$set(this.form, `product_type_name`, res[0].name);
      }
      this.$forceUpdate();
    },

    // 图片
    uploadSuccess({ type, data }) {
      let image = [{ url: data.uri }];
      this.$set(this.form, `image`, image);
    },
    // 删除图片
    uploadDelete(index) {
      this.form.image.splice(index, 1);
    },
  },
  computed: {
    ...mapState(['user']),
    pageTitle() {
      return `${this.$route.meta.title}`;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
};
</script>

<style lang="less" scoped>
.info {
  margin: 0 0px 50px 0;
  padding: 15px 0 0 0;
}
</style>
