<template>
  <div class="quickBuy">
    <div class="head-title">
        快速购买商品分类
        <el-button @click="openDig" style="float:right;margin-top: -5px;margin-left:20px;" size="small" type="primary">添加分类</el-button>
    </div>
    <div class="content">
        <div>
            <div class="searchInput" style="width:250px">
                <el-input style="display:inline-block;width:225px;height:30px"
                placeholder="请输入规格名搜索"
                v-model="msg.Name"
                @clear="getList"
                size="small"
                clearable>
                </el-input>
                <span @click="getList" class="el-icon-search" style="color:#979dad;font-size:14px;position:relative;top:1px"></span>
            </div>
            <span class="f12" style="padding:8px 15px;color:#909399;background-color: #f4f4f5;">温馨提示：商品分类修改至“
                <span @click="CommonJump('goodsClassification')" class="blue f14 point">商品管理-分类</span>
                ”修改
            </span>
        </div>
        <el-table
            :data="tableData"
            v-loading="loading"
            border
            style="width: 100%;margin:20px 0">
            <el-table-column
            prop="Id"
            width="80"
            label="ID">
            </el-table-column>
            <el-table-column
            prop="CategoryName"
            width="300"
            label="分类名称">
            </el-table-column>
            <el-table-column
            prop="address"
            label="图标">
                <template slot-scope="scope">
                    <img v-if="scope.row.IconPath!=''" style="width:50px;height:50px" :src="domainManager().ImageUrl+scope.row.IconPath" alt="">
                    <img v-else style="width:50px;height:50px" src="../../assets/img/default.png" alt="">
                </template>  
            </el-table-column>
            <el-table-column
            prop="address"
            label="排序">
                <template slot-scope="scope">
                    <template v-if="!scope.row.editState">
                        <span>{{scope.row.Sort}}</span>
                        <img @click="scope.row.editState=true,scope.row.SortNew=scope.row.Sort" style="width:15px;height:15px;margin-left:7px" src="../../assets/img/userman/edit1.png" alt="">
                    </template>
                    <template v-else>
                        <el-input v-model="scope.row.SortNew" type="number" min="1" size="small" style="display:inline-block;width:200px"></el-input>
                        <span @click="scope.row.editState=false" class="el-icon-error" style="color: rgb(245, 108, 108);"></span>
                        <span @click="Edit(scope.row)" class="el-icon-success" style="color:#67c23a"></span>
                    </template>
                </template>  
            </el-table-column>
            <el-table-column
            prop="CreateDate"
            width="220"
            label="添加日期">
            </el-table-column>
            <el-table-column
            prop="address"
            width="200"
            label="操作">
            <template slot-scope="scope">
                <img @click="Delete(scope.row)" style="width:32px;height:32px;margin:0 10px" src="../../assets/img/userman/del.png" alt="">
            </template>    
            </el-table-column>
        </el-table>
        <el-pagination style="text-align:right"
        background
        @current-change="handleCurrentChange"
        :page-size="msg.pageSize"
        layout="prev, pager, next"
        :total="total">
        </el-pagination>
    </div>
    <!-- 新增规格模板 -->
    <el-dialog title="新增" :visible.sync="ggmbDig" width="960px">
      <el-form :model="addMsg" :rules="rules" ref="addMsg" label-width="100px">
        
        <el-form-item label="分类" prop="CategoryIdNew">
            <el-cascader
            size="small"
            :show-all-levels="false"
            v-model="addMsg.CategoryIdNew"
            :options="options"
            :props="props"
            ></el-cascader>
        </el-form-item>
        <el-form-item label="排序" prop="Sort">
          <el-input size="small" v-model="addMsg.Sort" style="width:330px"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="ggmbDig = false">取 消</el-button>
        <el-button size="small" type="primary" @click="submitForm('addMsg')">确 定</el-button>
      </span>
    </el-dialog>
        
  </div>
</template>

<script>
export default {
    name: "quickBuy",
    data(){
        return{
            editState:false,
            ggmbDig:false,
            msg:{
                pageIndex:1,
                pageSize:15,
                Name:'',
                Id:0,
                Content:'',
            },
            total:0,
            tableData:[],
            loading:false,
            addMsg:{
                Id:0,
                Sort:'',
                CategoryIdNew:[],
                CategoryId:-1,
            },
            rules:{
                CategoryIdNew: [
                    { required: true, message: '分类不能为空', trigger: 'change' }
                ],
                Sort: [
                    { required: true, message: '请输入排序', trigger: 'blur' }
                ],
            },
            msg1:{
                Id:0,
                Name:'',
                Tier:0,
                ParentId:0,
                Enabled:1,
                IsShow:1,
            },
            props:{ 
                checkStrictly: true,
                label:'Name',
                value:'Id',
                children:'ChildList'
            },
            options:[],

        }
    },
    created(){
        this.getList();
        this.getTreeList();

    },
    methods:{
        Delete(item){
            let that=this;
            that.$confirm('是否删除?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.apipost('/api/product/DelProductQuickBuyInfo',{QuickBuyId:item.Id},
                    res => {
                    if (res.data.resultCode === 1) {
                        this.getList();
                        this.Success(res.data.message)

                    } else {
                        this.Error(res.data.message);
                    }
                    },
                    null
                );
            }).catch(() => {
                
            });
        },
        getTreeList(num){
            this.loading=true;
            this.apipost("/api/product/GetProductCategoryTreeList", this.msg1, res => {
                this.loading=false;
                if(res.data.resultCode==1){
                    let pageData=res.data.data;
                    this.options=pageData;
                }
                
            })
        },
        Edit(row){
            let addMsg=row;
            addMsg.Sort=addMsg.SortNew;
            this.apipost('/api/product/SetProductQuickBuyInfo',addMsg,
                res => {
                if (res.data.resultCode === 1) {
                    this.getList();
                    this.Success(res.data.message)

                } else {
                    this.Error(res.data.message);
                }
                },
                null
            );

        },
        deleteGgz(item,index){
            this.SpecList.splice(index,1);
        },
        addGgz(){
            let obj={num:''}
            this.SpecList.push(obj);
        },
        openDig(){
            this.addMsg={
                Id:0,
                Sort:'',
                CategoryIdNew:[],
                CategoryId:-1,
            }
            this.ggmbDig=true;
        },
        submitForm(formName){
            
            let index=this.addMsg.CategoryIdNew.length-1;
            this.addMsg.CategoryId=this.addMsg.CategoryIdNew[index];
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.apipost('/api/product/SetProductQuickBuyInfo',this.addMsg,
                        res => {
                        if (res.data.resultCode === 1) {
                            this.getList();
                            this.Success(res.data.message)
                            this.ggmbDig=false;

                        } else {
                            this.Error(res.data.message);
                        }
                        },
                        null
                    );
                } else {
                return false;
                }
            });
        },
        handleCurrentChange(val) {
            this.msg.pageIndex = val;
            this.getList();
        },
        getList(){
            this.loading=true;
            this.apipost("/api/product/GetProductQuickBuyPageList", this.msg, res => {
                this.loading=false;
                if(res.data.resultCode==1){
                    this.total=res.data.data.count;
                    let pageData=res.data.data.pageData;
                    pageData.forEach(item=>{
                        item.SortNew=item.Sort;
                        item.editState=false;
                    })
                    this.tableData=pageData;
                }
                
            })
        },
    },
};
</script>

<style>

.quickBuy .content .searchInput{
    border: 1px solid #DCDFE6;
    border-radius: 4px;
}
.quickBuy .content .searchInput .el-input__inner{
    border:none;outline:none;
    height: 30px;
    line-height: 30px;
}
.quickBuy .content .searchInput{
    line-height: normal;
    display: inline-table;
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    width:250px;
    margin-right: 20px;
}
.quickBuy .content{
    background: #fff;
    margin-top:10px;
    padding: 20px;
    box-sizing: border-box;
}
</style>
