<template>
  <div class="app-container">
    <div class="filter-container">

      <el-input v-model="search.keyWord" placeholder="关键字" style="width: 200px;" class="filter-item" />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="searchData">
        搜索
      </el-button>

      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-plus"
        @click="handleCreateAdd">
        增加发货方式
      </el-button>
    </div>
    <el-table
      :data="tableData"
      v-loading="listLoading"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      default-expand-all>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button @click="handleUpdate(row.id,row.modeName)" type="primary" size="mini">
            编辑
          </el-button>
          <el-button @click="editStatus(row.id,'0')" type="danger" v-if="row.enable==1" size="mini">
            禁用
          </el-button>
          <el-button @click="editStatus(row.id,'1')" v-else="row.enable==0" size="mini">
            启用
          </el-button>


        </template>
      </el-table-column>
      <el-table-column align="center" label="状态" class-name="status-col" width="100">
        <template slot-scope="{row}">
          <el-tag v-if="row.enable==1" type="success">
            启用
          </el-tag>
          <el-tag v-else="row.enable==0" type="danger">
            禁用
          </el-tag>
        </template>
      </el-table-column>
<!--      <el-table-column-->
<!--        label="创建时间"-->
<!--      >-->
<!--        <template slot-scope="scope">-->
<!--          <span v-if="scope.row.createTime">{{scope.row.createTime | getTime}}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column
        prop="modeName"
        label="发货方式名称">
      </el-table-column>



    </el-table>
    <pagination v-show="listData.total>0" :total="listData.total" :page.sync="listData.page" :limit.sync="listData.pageSize" @pagination="getTableInfo" />

    <el-dialog  :title="dialogTitle" :visible.sync="dialogVisible" width="500px">
      <el-form :model="ruleForm" label-width="130px" :rules="rules" ref="ruleForm" label-position="right">
        <el-form-item prop="modeName" label="发货方式名称">
          <el-input v-model="ruleForm.modeName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogStatus==='1'?addBtn('ruleForm'):editBtn('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>
    <!--    <servicedialog @tableRefresh="tableRefresh" ref="menuDialog" :dialogStatus="dialogStatus" :menuId="menuId" :titleDialog="titleDialog" :dialogVisible="dialogVisible"/>-->
    <cancledialog @tableRefresh="tableRefresh" ref="confirmDialog"></cancledialog>
  </div>
</template>

<script>
  import {getInfo,addInfo,updateInfo} from '@/api/doc/shipMode/shipMode.js'
  import Cancledialog from '@/components/Dialog/Cancledialog.vue'
  import Pagination from '@/components/Pagination'

  export default {
    components: {
      Cancledialog,Pagination
    },
    filters: {
      statusFilter(status) {
        const statusMap = {
          1: 'success',
          0: 'danger'
        }
        return statusMap[status]
      }
    },
    data() {
      return {
        dialogStatus: 1,//1增加，2修改
        dialogTitle: '',
        dialogVisible: false,
        search: {
          keyWord:''
        },
        ruleForm:{
          modeName:''
        },
        rules: {
          modeName: [
            { required: true, message: '请填写发货方式名称', trigger: 'blur' }
          ]

        },
        listData:{
          total:0,
          page: 1,
          pageSize:10
        },
        confirmdialog:false,
        listLoading: false,
        tableData: [],
        menuId:''
      }
    },
    mounted() {
      this.getTableInfo();
    },
    methods: {
      //获取列表
      getTableInfo() {
        const _that = this
        _that.listLoading = true
        var params = _that.search;
        var page = {
          pageIndex:_that.listData.page,
          pageSize: _that.listData.pageSize,
        }
        var data = {
          "params": JSON.stringify(params),
          "page": JSON.stringify(page),
        }
        // console.log(data);
        getInfo(data).then(response => {
          // console.log("tableData",response)
          const data = response.data
          // //  组合表单数据
          _that.tableData = data
          _that.listData.total = response.total
          // console.log(_that.tableData)
          _that.listLoading = false
        }).catch(err => {
          _that.listLoading = false
        })
      },
      //增加发货方式
      handleCreateAdd(){
        const _that = this
        _that.ruleForm={
          modeName:''
        }
        _that.dialogTitle = '增加发货方式'
        _that.dialogStatus = '1';
        _that.dialogVisible = true;

      },
      //刷新表格
      tableRefresh(){
        //刷新变革
        this.listData.page = 1;
        this.getTableInfo()
      },
      //增加
      addBtn(formName){
        const _that = this
        _that.$refs[formName].validate((valid) => {
          console.log(valid);
          if (valid) {
            var info =JSON.stringify(_that.ruleForm)
            console.log(info);
            var data = {
              "shipMode": info ,
            }

            // console.log("clientLevel",data);
            addInfo(data).then(res => {
              // console.log(res);
              if(res.httpCode == 200){
                _that.dialogVisible = false
                _that.$message({
                  message: '增加成功',
                  type: 'success'
                });
                _that.tableRefresh();
              }else{
                _that.$message.error(res.msg)
              }
            }).catch(err => {
              _that.$message.error(err)
              _that.dialogVisible = true
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });

      },

      //唤起修改
      handleUpdate(id,modeName){
        const _that = this;

        _that.dialogTitle = '修改发货方式'
        _that.dialogVisible = true
        _that.dialogStatus = '2';

          _that.ruleForm={
            id:id,
            modeName:modeName
          }
      },
      //修改档案
      editBtn(formName){
        const _that = this
        _that.$refs[formName].validate((valid) => {
          var info =JSON.stringify(_that.ruleForm)
          console.log(_that.ruleForm);
          var data = {
            "shipMode": info ,
          }
          // console.log(data);
          updateInfo(data).then(res => {
            // console.log(res);
            if(res.httpCode == 200){
              _that.dialogVisible = false
              _that.$message({
                message: '修改成功',
                type: 'success'
              });
              _that.tableRefresh();
            }else{
              _that.$message.error(res.msg)
            }
          }).catch(err => {
            _that.$message.error(err)
            _that.dialogVisible = true
          })
        })


      },

      //启用禁用
      editStatus(id,status){
        var _that = this;
        var data = {
          id:id,
          enable:status
        }
        var params = {
          "shipMode": JSON.stringify(data),
        }

        var url = '/doc/enableShipMode';
        _that.$refs.confirmDialog.updataClick(params, url);
      },

      //搜索
      searchData(){
        this.listData.page = 1;
        this.getTableInfo()
      }
    }
  }
</script>

<style lang="scss" scoped>
  .filter-container {
    margin-bottom: 15px;
  }
</style>
