<template>
    <!-- 弹出框表单 -->
    <el-drawer
            :title="title"
            :visible.sync="open"
            :direction="direction"
            :destroy-on-close="true"
            size="50%">
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-row>
              <Field :span="20"  label="类型种类名称" prop="typeName" v-model="form.typeName" 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="handleAddTypeDetails">添加</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteTypeDetails">删除</el-button>
                    </el-col>
                </el-row>

                <el-table :data="typeDetailsList" :row-class-name="rowTypeDetailsIndex" @selection-change="handleTypeDetailsSelectionChange" ref="typeDetails">
                    <el-table-column type="selection" width="50" align="center" />
                    <el-table-column label="序号" align="center" prop="index" width="50"/>
                            <el-table-column label="标题" prop="title">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.title" placeholder="请输入标题" />
                                </template>
                            </el-table-column>
                            <el-table-column label="图片" prop="imageUrl1">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.imageUrl1" placeholder="请输入图片" />
                                </template>
                            </el-table-column>
                            <el-table-column label="" prop="imageUrl2">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.imageUrl2" placeholder="请输入" />
                                </template>
                            </el-table-column>
                            <el-table-column label="" prop="imageUrl3">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.imageUrl3" placeholder="请输入" />
                                </template>
                            </el-table-column>
                            <el-table-column label="" prop="imageUrl4">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.imageUrl4" placeholder="请输入" />
                                </template>
                            </el-table-column>
                            <el-table-column label="" prop="imageUrl5">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.imageUrl5" placeholder="请输入" />
                                </template>
                            </el-table-column>
                            <el-table-column label="描述内容" prop="introduction">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.introduction" 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>

            <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: "TypeDetail",
        mixins: [form],
        components: {

        },
        created() {
            this.changePath("type")
        },
        data() {
            return {
                // 子表选中数据
                checkedTypeDetails: [],
                // 类型详细表格数据
                typeDetailsList: [],
                // 遮罩层
                loading: true,
                // 弹出层标题
                title: "类型",
                // 是否显示弹出层
                open: false,
                direction:"rtl",
                toString:[
                ],
                toDate:[
                ],
                // 表单校验
                rules: {
                }
            };
        },

        methods: {
            /** 类型详细序号 */
            rowTypeDetailsIndex({ row, rowIndex }) {
                row.index = rowIndex + 1;
            },
            /** 类型详细添加按钮操作 */
            handleAddTypeDetails() {
                let obj = {};
                obj.title = "";
                obj.imageUrl1 = "";
                obj.imageUrl2 = "";
                obj.imageUrl3 = "";
                obj.imageUrl4 = "";
                obj.imageUrl5 = "";
                obj.introduction = "";
                obj.createUserId = "";
                obj.createTime = "";
                obj.updateTime = "";
                this.typeDetailsList.push(obj);
            },
            /** 类型详细删除按钮操作 */
            handleDeleteTypeDetails() {
                if (this.checkedTypeDetails.length == 0) {
                    this.$alert("请先选择要删除的类型详细数据", "提示", { confirmButtonText: "确定", });
                } else {
                    this.typeDetailsList.splice(this.checkedTypeDetails[0].index - 1, 1);
                }
            },
            /** 单选框选中数据 */
            handleTypeDetailsSelectionChange(selection) {
                if (selection.length > 1) {
                    this.$refs.typeDetails.clearSelection();
                    this.$refs.typeDetails.toggleRowSelection(selection.pop());
                } else {
                    this.checkedTypeDetails = selection;
                }
            },
            // 渲染前置处理
            beforeRender(data) {
                if(data.entity.typeDetailsList) {
                    this.typeDetailsList = data.entity.typeDetailsList;
                }
                return data
            },
            /** 编辑 */
            edit(row) {
                this.reset()
                this.query = { id: row.id };
                this.urls.currUrl ="type/edit";
                this.getData();
                this.pageInfo.type="edit"
                this.title = "修改类型";
            },
            /** 新增 */
            add(row) {
                this.reset()
                this.urls.currUrl = "type/add";
                this.getData();
                this.pageInfo.type="add"
                this.title = "新增类型";
            },
            /** 查看*/
            view(row) {
                this.reset()
                this.query = { id: row.id };
                this.urls.currUrl ="type/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 = {
                    typeName : "0",
                };
                this.resetForm("form");
            },
            resetForm(refName) {
                if (this.$refs[refName]) {
                    this.$refs[refName].resetFields();
                }
            },
        },
    };
</script>
