<template>
    <!-- 弹出框表单 -->
    <el-dialog :title="title" :visible.sync="open" width="90%" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-row>
                                            <Field label="产品名称，名称唯一" prop="productName" v-model="form.productName" placeholder="请输入产品名称，名称唯一"/>
                            <Field label="产品编码" prop="productCode" v-model="form.productCode" type="textarea" placeholder="请输入产品编码"/>
                            <Field label="产品slogan" prop="productSlogan" v-model="form.productSlogan" placeholder="请输入产品slogan"/>
                            <Field label="产品图标" prop="productLogoPath" v-model="form.productLogoPath" placeholder="请输入产品图标"/>
                            <Field label="产品封面图片" prop="productFacePath" v-model="form.productFacePath" placeholder="请输入产品封面图片"/>
                            <Field label="产品视频,多个视频逗号分割" prop="productVideoPath" v-model="form.productVideoPath" type="textarea" placeholder="请输入产品视频,多个视频逗号分割"/>
                            <Field label="产品宣传图片,多个视频逗号分割" prop="productPicPath" v-model="form.productPicPath" type="textarea" placeholder="请输入产品宣传图片,多个视频逗号分割"/>
                            <Field label="产品介绍" prop="productIntroduction" v-model="form.productIntroduction" type="textarea" placeholder="请输入产品介绍"/>
                            <Field label="产品详情" prop="productDetail" v-model="form.productDetail" type="textarea" placeholder="请输入产品详情"/>
                            <Field label="发布时间" prop="publishTime" v-model="form.publishTime" type="date" />
                            <Field label="是否热门" prop="hot" v-model="form.hot" type="select" :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 :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="productName">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.productName" placeholder="请输入产品名称" />
                                </template>
                            </el-table-column>
                            <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-column label="创建时间" prop="createTime">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.createTime" placeholder="请输入创建时间" />
                                </template>
                            </el-table-column>
                            <el-table-column label="创建用户" prop="createUserId">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.createUserId" placeholder="请输入创建用户" />
                                </template>
                            </el-table-column>
                            <el-table-column label="修改时间" prop="updateTime">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.updateTime" placeholder="请输入修改时间" />
                                </template>
                            </el-table-column>
                </el-table>

        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" v-if="pageInfo.type !== 'view'" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import form from "@/assets/mixins/formdialog";
    import dialogShow from "./dialogshow";
    export default {
        mixins: [form],
        components: {
            dialogShow ,
        },
        data() {
            return {
                // 子表选中数据
                checkedProductQuestion: [],
                // 产品常见问题表格数据
                productQuestionList: [],
                // 遮罩层
                loading: true,
                // 弹出层标题
                title: "产品",
                // 是否显示弹出层
                open: false,
                toString:[
                    "hot",
                ],
                // 表单校验
                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;
            },

            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>
