<template>
  <el-dialog
    :title="dialogObj.title"
    :visible.sync="dialogObj.show"
    center
    width="800px">
    <el-form :model="form" :rules="rules" ref="form">
      <el-form-item label="合同协议名称" prop="contract_name" label-width="120px">
        <el-input v-model="form.contract_name" clearable></el-input>
      </el-form-item>
      <el-form-item label="类型" prop="contract_type" label-width="120px">
        <el-select v-model="form.contract_type" placeholder="请选择" clearable>
          <el-option label="用户协议" value="0"></el-option>
          <el-option label="正式课合同" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="关联商品" label-width="120px">
        <el-select multiple v-model="form.goods_ids" placeholder="请选择" clearable
                   :popper-class="'refresh-select-multi width-480'" style="width: 480px">
          <el-option
            v-for="data in goodslist"
            :key="data.id"
            :label="`【${data.id}】${data.name}`"
            :value="data.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="合同/协议文本" prop="" label-width="120px" required>
        <!--         <el-input type="textarea" :rows="10" v-model="form.contract_text"></el-input>-->
        <editor-detail :lookData="form.contract_text"/>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
    <el-button @click="dialogObj.show = false">取 消</el-button>
    <el-button type="primary" @click="sub">保 存</el-button>
  </span>
  </el-dialog>
</template>

<script>
    import {contractGoodslistApi, contractAddApi, contractEditApi} from "../../service/api";
    import editorDetail from "./editorDetail";

    export default {
        name: "dialogObj",
        props: [
            'dialogObj'
        ],
        components: {
            editorDetail
        },
        data() {
            return {
                form: {
                    contract_name: '',
                    contract_type: '',
                    goods_ids: [],
                    contract_text: {
                        detail: ""
                    }
                },
                goodslist: [],
                rules: {
                    contract_name: [
                        {required: true, message: '请输入赠品名称'},
                        {max: 20, message: '合同协议名称不能超过20汉字'}
                    ],
                    contract_type: [
                        {required: true, message: '请选择类型', trigger: 'change'}
                    ]
                }
            }
        },
        methods: {
            sub() {
                if (!this.form.contract_text.detail || this.form.contract_text.detail == "<!DOCTYPE html>\n<html>\n<head>\n</head>\n<body>\n\n</body>\n</html>") {
                    this.$message({
                        message: '合同/协议文本不能为空',
                        type: 'error'
                    });
                    return;
                }
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        let json = {
                            contract_name: this.form.contract_name,
                            contract_type: this.form.contract_type,
                            goods_ids: this.form.goods_ids.join(','),
                            contract_text: this.form.contract_text.detail
                        };
                        switch (this.dialogObj.type) {
                            case 0:
                                contractAddApi(json).then(res => {
                                    this.$message({
                                        type: 'success',
                                        message: '新增成功!'
                                    });
                                    this.$emit("reflash");
                                    this.dialogObj.show = false;
                                });
                                break;
                            case 1:
                                contractEditApi(this.dialogObj.id, json).then(res => {
                                    this.$message({
                                        type: 'success',
                                        message: '修改成功!'
                                    });
                                    this.$emit("reflash");
                                    this.dialogObj.show = false;
                                });
                                break;
                        }
                    } else {
                        return false;
                    }
                });
            }
        },
        created() {
            contractGoodslistApi().then(res => {
                this.goodslist = res.goods_list;
            });
        },
        watch: {
            'dialogObj.show'() {
                this.$nextTick(() => {
                    this.form.goods_ids = [];
                    this.form.contract_text.detail = '';
                    this.$refs['form'].resetFields();
                    if (this.dialogObj.type == 1) {
                        let goods_ids_arr = [];
                        if (this.dialogObj.goods_ids) {
                            this.dialogObj.goods_ids.split(',').map(i => {
                                goods_ids_arr.push(Number(i));
                            });
                        }
                        this.form.name = this.dialogObj.name;
                        this.form.contract_name = this.dialogObj.contract_name;
                        this.form.contract_type = this.dialogObj.contract_type.toString();
                        this.form.goods_ids = goods_ids_arr;
                        this.form.contract_text.detail = this.dialogObj.contract_text;
                    }
                });
            }
        }
    }
</script>

<style scoped>

</style>
