<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" >
            <div class="form">
                <div class="formleft">
                    <el-row>
                    <Field label="产品名称" prop="productName" v-model="form.productName" placeholder="请输入产品名称，名称唯一"/>
                    </el-row>
                    <el-row>
                    <Field label="产品slogan" prop="productSlogan" v-model="form.productSlogan" placeholder="请输入产品slogan"/>
                    </el-row>
                    <el-row>
                    <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> -->
                    </el-row>
                    <el-row>
                    <Field label="产品封面图片" prop="productFacePath" ><imageUpload v-model="form.productFacePath" prePath="/file/preview"/> </Field>
                    </el-row>
                    <el-row>
                    <Field label="宣传形式" type="radio" :enumData="propagandaList" v-model="nowPropaganda" :field="nowPropaganda" @emit="changeNowprop"></Field>
                    </el-row>
                    <el-row>
                    <Field label="轮播图" v-if="nowPropaganda==0"><imageUpload v-model="form.productPicPath" prePath="/file/preview"/></Field>
                    </el-row>
                    <el-row>
                    <Field label="短视频" v-if="nowPropaganda==1"><fileUpload v-model="form.productVideoPath"  prePath="/file/fileupload"/></Field>
                    </el-row>
                    <el-row>
                    <Field label="产品简介" prop="productIntroduction" v-model="form.productIntroduction" type="textarea" placeholder="请输入产品简介"/>
                    <!-- <Field label="发布时间" prop="publishTime" v-model="form.publishTime" type="date" /> -->
                    </el-row>
                    <el-row>
                    <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>
                </div>
                
                <div class="formright">
                    <div class="tabs">
                        <div class="tab" :class="nowtab==1?'activetab':''" @click="changeTab(1)">产品详情</div>
                        <div class="tab" :class="nowtab==2?'activetab':''" @click="changeTab(2)">常见问题</div>
                    </div>
                    <div class="content" v-show="nowtab==1">
                        <editor v-model="form.productDetail" :min-height="100"/>
                    </div>
                    <div class="content" v-show="nowtab==2">
                        <el-table :data="productQuestionList" :row-class-name="rowProductQuestionIndex" @selection-change="handleProductQuestionSelectionChange" ref="productQuestion">
                            <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-column label="操作" width="240" align="center">
                                <template slot-scope="scope">
                                <Confirm @confirm="handleDeleteCompanyPatents(scope.row)" message="确定要删除该条知识产权吗？">
                                    <div class="del">删除</div>
                                </Confirm>
                                </template>
                            </el-table-column>
                        </el-table>
                        
                        <el-row style="margin:10px 0;display: flex;justify-content: center;" :gutter="10" class="mb8">
                            <el-col :span="1.5">
                                <el-button type="primary" icon="el-icon-plus" @click="handleAddProductQuestion">新增常见问题</el-button>
                            </el-col>
                        </el-row>

                    </div>
                </div>
            </div>
            
            
                <!-- 保存按钮 -->
            <form-buttons @submit='onSubmitForm' @cancel='cancelForm' v-if="pageInfo.type!='view'" class="formbtns" />
        </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 {
                propagandaList:['轮播图','短视频'],
                // 当前宣传形式
                nowPropaganda:0,
                nowtab:1,
                // 子表选中数据
                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: {
            // 重写提交表单
            onSubmitForm(){
                // 由于宣传形式修改，只能存在一种，所以当选中一种时另外一种清空
                
                if(this.nowPropaganda===0){
                    // 选中轮播图
                    this.form.productVideoPath = ''
                }else{
                    // 选中短视频
                    this.form.productPicPath = ''
                }
                this.submitForm()
            },
            // 修改宣传形式
            changeNowprop(val){
                this.nowPropaganda = val
            },
            // 问题删除
            handleDeleteCompanyPatents(row) {
                console.log(row);
                // 由于有一些知识产权还没有id，所以这里删除选用的是字段中带的index
                this.productQuestionList = this.productQuestionList.filter(
                    item => item.index !== row.index
                );
                this.form.productQuestionList = this.productQuestionList;
            },
            cancelForm(){
                this.open = false
            },
            changeTab(nowtab){
                this.nowtab = nowtab
                this.$forceUpdate()
            },
            /** 产品常见问题序号 */
            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;
                }
                // 判断有无短视频，如果有短视频那么更改为短视频选项
                if(data.entity.productVideoPath){
                    this.nowPropaganda = 1
                }else{
                    this.nowPropaganda = 0
                }
                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>
<style lang="less" scoped>
.del {
  color: red;
}
.formbtns {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  height: 60px;
  margin-top: 30px;
  padding-right: 30px;
  border-top: 1px solid #dcdcdc;
}
.tabs{
    background: #dcdcdc59;
    height: 50px;
    display: flex;
    padding-left: 10px;
    .tab{
        height: 100%;
        line-height: 50px;
        padding: 0 20px;
        width: 120px;
        align-items: center;
        // display: flex;
        // justify-content: center;
        // align-content: center;
    }
    .activetab{
        background: white;
    }
}
.form{
    display: flex;
    .formleft{
        width: 600px;
        border-right: 1px solid gainsboro;
        flex-shrink: 0;
        flex-grow: 0;
    }
    .el-col-12{
      width: 96% !important;
    }
    .formright{
        margin-left: 10px;
        width: 100%;
        .content{
            margin-top: 10px;
            width: 100%;
        }
    }
}
</style>