<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item prop="flawId">
                <el-select v-model="queryParams.flawId" placeholder="瑕疵" clearable>
                    <el-option v-for="item in flawList" :label="item.name" :value="item.id" :key="item.name"> </el-option>
                </el-select>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <Page uri="/mapi/core/flawTop/list" :request-params="queryParams" ref="pagination">
            <el-table-column label="衣服瑕疵置顶" align="center" prop="id" v-if="false" />
            <el-table-column label="瑕疵id" align="center" prop="flawId" v-if="false" />
            <el-table-column label="瑕疵名称" align="center" prop="flawName" />
            <el-table-column label="瑕疵类型" align="center" prop="flawTypeName" />
            <el-table-column label="目标id" align="center" prop="targetId" v-if="false" />
            <el-table-column label="关联目标类型 " align="center" prop="type" v-if="false" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" prop="isCheck">
                <template slot-scope="scope">
                    <el-switch v-model="scope.row.isCheck" :active-value="1" :inactive-value="0" @change="handleUpdate(scope.row)" v-hasPermi="['cloth:flawTop:edit']">置顶</el-switch>
                </template>
            </el-table-column>
        </Page>
    </div>
</template>

<script>
import { listFlawTop, getFlawTop, delFlawTop, addFlawTop, updateFlawTop } from '@/api/core/flawTop'
import { listFlaw } from '@/api/core/flaw'

export default {
    name: 'FlawTop',
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 衣服瑕疵置顶表格数据
            flawTopList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                flawId: null,
                targetId: null,
                type: null,
                createById: null,
                updateById: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {},
            flawList: []
        }
    },
    created() {
        this.getFlawList()
        this.getList()
    },
    methods: {
        /** 获取所属瑕疵类型 */
        getFlawList() {
            listFlaw().then((response) => {
                this.flawList = response.rows
            })
        },
        /** 查询衣服瑕疵置顶列表 */
        getList() {
            this.$nextTick(() => {
                this.$refs.pagination.handleSearch(true)
            })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                flawId: null,
                targetId: null,
                type: null,
                createBy: null,
                createById: null,
                createTime: null,
                updateBy: null,
                updateById: null,
                updateTime: null,
                remark: null
            }
            this.resetForm('form')
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm')
            this.handleQuery()
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            let text = row.isCheck === 0 ? '取消置顶' : '置顶'
            this.$modal
                .confirm('确认要' + text + ' ' + row.flawName + ' 吗？')
                .then(function () {
                    return updateFlawTop(row)
                })
                .then(() => {
                    this.$modal.msgSuccess(text + '成功')
                })
                .catch(function () {
                    row.isCheck = row.isCheck === 0 ? 1 : 0
                })
        }
    }
}
</script>
