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

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

      <el-button
        size="mini"
        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
      size="mini"
      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.payMode)" type="primary" size="mini">
            编辑
          </el-button>
          <el-button @click="editStatus(row.id)" size="mini">
            删除
          </el-button>


        </template>
      </el-table-column>
      <el-table-column
        prop="cutName"
        label="档案名称">
      </el-table-column>
      <el-table-column
        prop="cutCount"
        label="开数">
      </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>
    <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">
      <div style="clear: both"></div>
      <el-form :model="ruleForm" label-width="130px" :rules="rules" ref="ruleForm" label-position="right">
        <el-form-item prop="cutName" label="档案名称">
          <el-input v-model="ruleForm.cutName"></el-input>
        </el-form-item>
        <el-form-item prop="cutCount" label="开数">
          <el-input v-model="ruleForm.cutCount"></el-input>
        </el-form-item>
      </el-form>
      <div style="width: 400px;margin-left: 50px;position: relative">
        <div @click="addPaperSize" style="position: absolute;z-index: 99999;right: 10px;top: 8px;font-size: 16px;font-weight: bold;color: #409EFF;cursor: pointer;"><i class="el-icon-plus"></i></div>
        <el-table
          class="tb-edit"
          row-key="id"
          highlight-current-row
          @row-click="handleCurrentChange"
          border
          size="mini"
          width="400px"
          :data="sizeInfoList"
          style="width: 100%">
          <el-table-column fixed="left" label="操作" align="center" width="80" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <i style="cursor: pointer" @click="deleteSizeInfo(scope.$index)" class="el-icon-delete"></i>
            </template>
          </el-table-column>
          <el-table-column
            label="#"
            align="center"
            type="index"
            :index="indexMethod">
          </el-table-column>
          <el-table-column
            label="大纸尺寸">
            <template slot-scope="scope">
              <el-input size="mini" v-model="scope.row.wholePaperSize" placeholder="请输入内容"
                        @change="handleEdit(scope.$index, scope.row.wholePaperSize)"></el-input>
              <span>{{ scope.row.wholePaperSize }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="开纸尺寸">
            <template slot-scope="scope">
              <el-input size="mini" v-model="scope.row.cutPaperSize" placeholder="请输入内容"
                        @change="handleEdit(scope.$index, scope.row.cutPaperSize)"></el-input>
              <span>{{ scope.row.cutPaperSize }}</span>
            </template>
          </el-table-column>

        </el-table>
      </div>
      <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, getInfoById,deletePaperCutInfo} from '@/api/baseInfo/paperCutInfoList/paperCutInfoList.js'
import Cancledialog from '@/components/Dialog/Cancledialog.vue'


export default {
  components: {
    Cancledialog
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        1: 'success',
        0: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      innerVisible:false,
      dialogStatus: 1,//1增加，2修改
      dialogTitle: '',
      dialogVisible: false,
      search: {
        keyWord: ''
      },
      ruleForm: {},
      rules: {
        cutName: [
          {required: true, message: '请填写档案名称', trigger: 'blur'}
        ],
        cutCount: [
          {required: true, message: '请填写开数', trigger: 'blur'}
        ]
      },
      innerForm: {},
      innerRules: {
        wholePaperSize: [
          {required: true, message: '请填写大纸尺寸', trigger: 'blur'}
        ],
        cutPaperSize: [
          {required: true, message: '请填写开纸尺寸', trigger: 'blur'}
        ]
      },
      listData: {
        total: 0,
        page: 1,
        pageSize: 10
      },
      confirmdialog: false,
      listLoading: false,
      tableData: [],
      sizeInfoList:[],
      menuId: ''
    }
  },
  mounted() {
    this.getTableInfo();
  },
  methods: {
    deleteSizeInfo(index){
      const _that = this;
      _that.sizeInfoList.splice(index,1)
    },
    addPaperSize(){
      //增加开纸尺寸
      const _that = this;
      var info = {
        cutPaperSize:"",
        wholePaperSize:""
      }
      _that.sizeInfoList.push(info);

    },
    //获取列表
    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 = {};
      _that.dialogTitle = '增加纸张开数档案';
      _that.dialogStatus = '1';
      _that.dialogVisible = true;
      _that.sizeInfoList = [];
    },
    //刷新表格
    tableRefresh() {
      //刷新变革
      this.getTableInfo()
    },
    //增加
    addBtn(formName) {
      const _that = this
      _that.$refs[formName].validate((valid) => {
        // console.log(valid);
        if (valid) {
          // if(_that.sizeInfoList.length<=0){
          //   _that.$message("请增加纸张开数尺寸");
          //   return
          // }
          var infoData = {
            cutName: _that.ruleForm.cutName,
            cutCount: _that.ruleForm.cutCount,
            sizeInfoList: _that.sizeInfoList
          }
          var info = JSON.stringify(infoData)
          // console.log(info);
          var data = {
            "paperCutInfo": 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) {
      const _that = this;
      // console.log(id);
      _that.dialogTitle = '修改纸张开数档案'
      _that.dialogVisible = true
      _that.dialogStatus = '2';
      _that.byId = id;
      var data = {
        idCutInfo: id
      }

      getInfoById(data).then(res => {
        // console.log(res);
        var datas = res.data;
        // console.log(datas);
        _that.sizeInfoList = datas.sizeInfoList;
        _that.ruleForm = {
          id: id,
          cutName: datas.cutName,
          cutCount: datas.cutCount
        }

      }).catch(err => {
        _that.$message.error(err)
      })
    },
    //修改档案
    editBtn(formName) {
      const _that = this
      _that.$refs[formName].validate((valid) => {
        // console.log(_that.ruleForm);
        // if(_that.sizeInfoList.length<=0){
        //   _that.$message("请增加纸张开数尺寸");
        //   return
        // }
        var infoData = {
          id:_that.ruleForm.id,
          cutName: _that.ruleForm.cutName,
          cutCount: _that.ruleForm.cutCount,
          sizeInfoList: _that.sizeInfoList
        }
        var info = JSON.stringify(infoData)
        // console.log(info);
        var data = {
          "paperCutInfo": 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) {
      var _that = this;
      var params = {
        "idCutInfo": id
      }

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

    //搜索
    searchData() {
      this.listData.page = 1;
      this.getTableInfo()
    },
    indexMethod(index) {
      return index+1;
    },
    //行内编辑
    handleCurrentChange(row, event, column) {
      // console.log(row, event, column, event.currentTarget);
    },
    handleEdit(index, row) {
      const _that = this;
    },
  }
}
</script>

<style>

</style>
<style lang="scss" scoped>
.tb-edit .el-input {
  display: none
}

.tb-edit .current-row .el-input {
  display: block
}

.tb-edit .current-row .el-input + span {
  display: none
}
.el-table .cell{
  min-height: 23px;
}
.filter-container {
  margin-bottom: 15px;
}
</style>
