<template>
    <!-- 弹出框表单 -->
    <el-drawer
            :title="title"
            :visible.sync="open"
            :direction="direction"
            :destroy-on-close="true"
            size="90%">
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-row>
              <Field label="产品名称" prop="productName" v-model="form.productName" placeholder="请输入产品名称，名称唯一"/>
                           <Field label="产品slogan" prop="productSlogan" v-model="form.productSlogan" placeholder="请输入产品slogan"/>

              <Field label="产品分类" prop="categoryId" v-model="form.categoryId" :multiple="true" type="select" :enumData="dict.categoryId" placeholder="请选择产品分类"/>
              <Field label="所属企业" prop="companyId" v-model="form.companyId" :multiple="true" type="select" :enumData="dict.companyId" placeholder="请选择所属企业"/>


              <Field label="产品图标"><imageUpload v-model="form.productLogoPath" prePath="/file/preview"/></Field>
              <Field label="产品封面图片" prop="productFacePath" ><imageUpload v-model="form.productFacePath" prePath="/file/preview"/> </Field>
                            <Field label="产品宣传图片"><imageUpload v-model="form.productPicPath" prePath="/file/preview"/></Field>
              <Field label="产品视频"><fileUpload v-model="form.productVideoPath"  prePath="/file/fileupload"/></Field>
              <Field label="产品介绍" prop="productIntroduction" v-model="form.productIntroduction" type="textarea" placeholder="请输入产品介绍"/>
              <Field label="产品详情" prop="productDetail"   placeholder="请输入产品详情"><editor v-model="form.productDetail" :min-height="256"/></Field>
                            <Field label="发布时间" prop="publishTime" v-model="form.publishTime" type="date" />
                            <Field label="热门" prop="hot" v-model="form.hot" type="radio" :enumData="dict.hot" placeholder="请选择是否热门"/>
                            <Field label="备注" prop="productRemark" v-model="form.productRemark" type="textarea" placeholder="请输入备注"/>

            </el-row>
                <el-divider content-position="center">产品常见问题信息</el-divider>
                <br/>
                <el-row style="margin-left: 10px" :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddProductQuestion">添加</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteProductQuestion">删除</el-button>
                    </el-col>
                </el-row>

                <el-table :data="productQuestionList" :row-class-name="rowProductQuestionIndex" @selection-change="handleProductQuestionSelectionChange" ref="productQuestion">
                    <el-table-column type="selection" width="50" align="center" />
                    <el-table-column label="序号" align="center" prop="index" width="50"/>
                            <el-table-column label="问题" prop="question">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.question" placeholder="请输入问题" />
                                </template>
                            </el-table-column>
                            <el-table-column label="常见问题回答" prop="answer">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.answer" placeholder="请输入常见问题回答" />
                                </template>
                            </el-table-column>

                </el-table>

            <form-buttons @submit='submitForm' v-if="pageInfo.type!='view'" noCancelBtn />
        </el-form>

    </el-drawer>
</template>

<script>
    import form from "@/assets/mixins/formdialog";
    export default {
        name: "ProductDetail",
        mixins: [form],
        components: {

        },
        created() {
            this.changePath("product")
        },
        data() {
            return {
                // 子表选中数据
                checkedProductQuestion: [],
                // 产品常见问题表格数据
                productQuestionList: [],
                // 遮罩层
                loading: true,
                // 弹出层标题
                title: "产品",
                // 是否显示弹出层
                open: false,
                direction:"rtl",
                toString:[
                    "hot",
                ],
              toArrays:[
                "companyId",
                "categoryId"
              ],
                toDate:[
                    "publishTime",
                ],
                // 表单校验
                rules: {
                    productName: [
                        {required: true,message: "请输入产品名称，名称唯一", trigger: "blur" },
                        {max: 20,message: "最多只能录入20个字符",trigger: "blur",},
                    ],
                    productSlogan: [
                        {required: true,message: "请输入产品slogan", trigger: "blur" },
                        {max: 64,message: "最多只能录入64个字符",trigger: "blur",},
                    ],
                    productLogoPath: [
                        {required: true,message: "请输入产品图标", trigger: "blur" },
                        {max: 64,message: "最多只能录入64个字符",trigger: "blur",},
                    ],
                    productFacePath: [
                        {required: true,message: "请输入产品封面图片", trigger: "blur" },
                        {max: 64,message: "最多只能录入64个字符",trigger: "blur",},
                    ],
                    publishTime: [
                        {required: true,message: "请选择发布时间" },
                    ],
                    createTime: [
                        {required: true,message: "请选择创建时间" },
                    ],
                }
            };
        },

        methods: {
            /** 产品常见问题序号 */
            rowProductQuestionIndex({ row, rowIndex }) {
                row.index = rowIndex + 1;
            },
            /** 产品常见问题添加按钮操作 */
            handleAddProductQuestion() {
                let obj = {};
                obj.productName = "";
                obj.question = "";
                obj.answer = "";
                obj.createTime = "";
                obj.createUserId = "";
                obj.updateTime = "";
                this.productQuestionList.push(obj);
            },
            /** 产品常见问题删除按钮操作 */
            handleDeleteProductQuestion() {
                if (this.checkedProductQuestion.length == 0) {
                    this.$alert("请先选择要删除的产品常见问题数据", "提示", { confirmButtonText: "确定", });
                } else {
                    this.productQuestionList.splice(this.checkedProductQuestion[0].index - 1, 1);
                }
            },
            /** 单选框选中数据 */
            handleProductQuestionSelectionChange(selection) {
                if (selection.length > 1) {
                    this.$refs.productQuestion.clearSelection();
                    this.$refs.productQuestion.toggleRowSelection(selection.pop());
                } else {
                    this.checkedProductQuestion = selection;
                }
            },
            // 渲染前置处理
            beforeRender(data) {
                if(data.entity.productQuestionList) {
                    this.productQuestionList = data.entity.productQuestionList;
                }
                return data
            },
            /** 编辑 */
            edit(row) {
                this.reset()
                this.query = { id: row.id };
                this.urls.currUrl ="product/edit";
                this.getData();
                this.pageInfo.type="edit"
                this.title = "修改产品";
            },
            /** 新增 */
            add(row) {
                this.reset()
                this.urls.currUrl = "product/add";
                this.getData();
                this.pageInfo.type="add"
                this.title = "新增产品";
            },
            /** 查看*/
            view(row) {
                this.reset()
                this.query = { id: row.id };
                this.urls.currUrl ="product/view";
                this.getData();
                this.pageInfo.type="view"
                this.title = "产品详细";
            },
            /**取消按钮 */
            cancel() {
                this.open = false;
            },
            /**获取数据后弹框 */
            afterRender(data) {
                this.open = true;
            },

          beforeSubmit(data) {

              data.categoryId=data.categoryId.join(",")
              data.companyId=data.companyId.join(",")

            return data
          },


            afterSubmit(data) {
                this.open = false;
                this.$emit("ok");
            },

            // 表单重置
            reset() {
                this.form = {
                    productName : "",
                    productCode : "",
                    productSlogan : "",
                    productLogoPath : "",
                    productFacePath : "",
                    productVideoPath : "",
                    productPicPath : "",
                    productIntroduction : "",
                    productDetail : "",
                    publishTime : null,
                    hot : 0,
                    productRemark : "",
                };
                this.resetForm("form");
            },
            resetForm(refName) {
                if (this.$refs[refName]) {
                    this.$refs[refName].resetFields();
                }
            },
        },
    };
</script>
