<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
        prop="id"
        label="字典编号">
      </el-table-column>
      <el-table-column label="字典名称" align="center" prop="dictName" :show-overflow-tooltip="true" />
      <el-table-column label="字典类型" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
<!--          <router-link :to="{path:'/system/dictData',query:{typeId:scope.row.id,dictType:scope.row.dictType}}" class="link-type">-->
          <el-link  @click="$router.push({path:'/system/dictData/'+scope.row.id ,query:{typeId:scope.row.id,dictType:scope.row.dictType}})" type="primary">{{ scope.row.dictType }}</el-link>
<!--          </router-link>-->
        </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.voidFlag==0" type="success">
            正常
          </el-tag>
          <el-tag v-else="row.voidFlag==1" type="danger">
            禁用
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
<!--      <el-table-column label="创建时间" align="center" prop="createTime" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.createTime) }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="操作" fixed="left" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button @click="handleUpdate(row)" type="primary" size="mini">
            编辑
          </el-button>
          <el-button @click="editStatus(row.id,'1')" type="danger" v-if="row.voidFlag==0" size="mini">
            禁用
          </el-button>
          <el-button @click="editStatus(row.id,'0')" v-else="row.voidFlag==1" size="mini">
            启用
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="listData.total>0" :total="listData.total" :page.sync="listData.page" :limit.sync="listData.pageSize" @pagination="getTypeList" />


    <el-dialog :close-on-click-modal="false" :title="dialogTitle" :visible.sync="dialogVisible" width="600px">
      <el-form label-width="130px" label-position="right">
        <el-form-item label="字典名称" prop="dictName">
          <el-input v-model="form.dictName" placeholder="请输入字典名称" />
        </el-form-item>
        <el-form-item label="字典类型" prop="dictType">
          <el-input v-model="form.dictType" placeholder="请输入字典类型" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></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():editBtn()">确 定</el-button>
      </div>
    </el-dialog>
    <cancledialog @tableRefresh="tableRefresh" ref="confirmDialog"></cancledialog>
  </div>
</template>

<script>
  import {listType,addType,getTypeById,updateType} from '@/api/system/dict/type.js'
  import Cancledialog from '@/components/Dialog/Cancledialog.vue'

  const form={
    id: undefined,
    dictName: undefined,
    dictType: undefined,
    remark: undefined
  };

  export default {
    name:'dictData',
    components: {
      Cancledialog
    },
    data() {
      return {
        dialogStatus: 1,//1增加，2修改
        dialogTitle: '',
        dialogVisible: false,

        innerVisible:false,

        search: {
          keyWord:''
        },
        form:Object.assign({}, form),

        confirmdialog:false,
        listLoading: false,
        tableData: [],

        listData:{
          total:0,
          page: 1,
          pageSize:10
        },


      }
    },
    mounted() {
      this.getTypeList();
    },
    methods: {
      //获取列表
      getTypeList() {
        const _that = this
        _that.listLoading = true
        let params = _that.search;
        var page = {
          pageIndex:_that.listData.page,
          pageSize: _that.listData.pageSize,
        }
        let data = {
          "params": JSON.stringify(params),
          "page": JSON.stringify(page),
        }
        // console.log(data);
        listType(data).then(response => {
          console.log(response)
          const data = response.data
          // //  组合表单数据
          _that.tableData = data
          console.log(_that.tableData)
          _that.listData.total = response.total
          _that.listLoading = false
        }).catch(err => {
          _that.$message.error(err);
          _that.listLoading = false
        })
      },

      //增加字典管理
      handleCreateAdd(){
        const _that = this
        _that.form=Object.assign({},form)
        _that.dialogTitle = '增加字典管理'
        _that.dialogStatus = '1';
        _that.dialogVisible = true;
      },
      //增加档案
      addBtn(){
        const _that = this;
        let info =JSON.stringify(_that.form)
        let data = {
          "dictType": info ,
        }
        // console.log(data);
        // return

        addType(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
        })
      },

      //唤起修改
      handleUpdate(row){
        const _that = this;

        _that.dialogTitle = '修改字典管理';
        _that.dialogVisible = true;
        _that.dialogStatus = '2';
        let idPrint = row.id
        let data= {
          id : idPrint
        }
        // console.log(data);
        getTypeById(data).then(res => {
          console.log(res);
          const data = res.data

          _that.form={
            id:data.id,
            dictName: data.dictName,
            dictType: data.dictType,
            voidFlag: data.voidFlag,
            remark: data.remark
          }
        }).catch(err => {
          _that.$message.error(err)
        })
      },
      //修改档案
      editBtn(){
        const _that = this;
        let info =JSON.stringify(_that.form)
        let data = {
          "dictType": info ,
        }
        // console.log(data);
        // return;

        updateType(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
        })

      },


      //刷新变革
      tableRefresh(){
        //刷新变革
        this.getTypeList()
      },

      //启用禁用
      editStatus(id,status){
        let _that = this;
        let printerTypeInfo = {
          id:id,
          voidFlag:status
        }
        let params = {
          "dictType": JSON.stringify(printerTypeInfo),
        }

        let url = '/system/voidDictType';
        _that.$refs.confirmDialog.updataClick(params, url);
      },

      //搜索
      searchData(){
        this.listData.page = 1;
        this.getTypeList()
      },

    }
  }
</script>

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