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

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

      <el-button
        v-if="webType && isChoose == false"
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-plus"
        @click="handleCreateAdd">
        增加担保人档案
      </el-button>
      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="getPaperInfo" />
        </el-tooltip>
      </div>
    </div>
    <el-table
      :data="tableData"
      v-loading="listLoading"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      :size="!webType ? 'mini':''"
      border
      default-expand-all>
      <el-table-column
        prop="personName"
        label="担保人">
      </el-table-column>
      <el-table-column
        prop="moneyLimit"
        label="担保金额">
      </el-table-column>
      <template v-if="webType">
        <el-table-column
          prop="overMoney"
          label="剩余担保金额">
        </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-if="row.voidFlag==1" type="danger">
            禁用
          </el-tag>
        </template>
      </el-table-column>
      </template>
      <el-table-column label="操作" fixed="left" align="center" width="300" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <template v-if="webType">
            <template v-if="!isChoose">
          <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-if="row.voidFlag==1" size="mini">
            启用
          </el-button>
            </template>
            <template v-else>
              <el-button @click="sourceType == 1? chooseThisTo(row):chooseThis(row)" type="primary" size="mini">
                选择
              </el-button>
            </template>
          </template>
          <template v-else>
            <el-button @click="ERPChoose(row)" type="primary" size="mini">
              选择
            </el-button>
          </template>
        </template>
      </el-table-column>

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

    <el-dialog :title="dialogTitle" :close-on-click-modal="false" :visible.sync="dialogVisible" width="600px">
      <el-dialog title="选择担保人" :visible.sync="userVisible" append-to-body width="800px">
        <user :is-choose="true" @chooseUser = 'chooseUser'></user>
      </el-dialog>
      <el-form label-width="130px" label-position="right">
            <el-form-item label="担保人">
              <el-button :disabled="dialogStatus == 2" @click="openChoose">{{form.personName ? form.personName : '选择人员'}}</el-button>
            </el-form-item>
            <el-form-item label="担保金额">
              <el-input v-model="form.moneyLimit" ></el-input>
            </el-form-item>
            <el-form-item label="担保说明">
              <el-input type="textarea" :rows="2" v-model="form.memo"></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 { getGuarantorInfoList, getGuarantorInfo, updateGuarantorInfo, addGuarantorInfo } from '@/api/baseInfo/guarantor'
import Cancledialog from '@/components/Dialog/Cancledialog.vue'
import User from '@/views/system/user/user'

const form = {
  'idPerson': '', // 人员id,从人员档案选取,
  'memo': '', // "担保说明",
  'moneyLimit': '', // 担保金额,
  'personName': '' // 人员名称,
}
export default {
  name: 'Guarantor',
  components: {
    User,
    Cancledialog
  },
  props: {
    isChoose: {
      type: Boolean,
      default: false
    },
    /**
     * 调用来源
     * 0 普通调用
     * 1 查询统计调用
     */
    sourceType: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      clickLoading: false,
      webType: true,
      infoVisible: false,
      userVisible: false,
      dialogStatus: 1, // 1增加，2修改
      dialogTitle: '',
      dialogVisible: false,
      search: {
        keyWord: ''
      },
      form: Object.assign({}, form),
      options: [],

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

      confirmdialog: false,
      listLoading: false,
      tableData: [],
      basePaperName: ''
    }
  },
  mounted() {
    this.getPaperInfo()
    if (this.$route.query.type === undefined) {
      this.webType = true
    } else {
      this.webType = false
    }
  },
  methods: {
    // 获取列表
    getPaperInfo() {
      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);
      getGuarantorInfoList(data).then(response => {
        console.log(response)
        const data = response.data
        // //  组合表单数据
        _that.tableData = data
        _that.listData.total = response.total
        // console.log(_that.tableData)
        _that.listLoading = false
      }).catch(err => {
        _that.$message.error(err)
        _that.listLoading = false
      })
    },

    // 刷新变革
    tableRefresh() {
      // 刷新变革
      this.getPaperInfo()
    },
    // 增加担保人档案
    handleCreateAdd() {
      // 增加系统目录
      const _that = this
      _that.clickLoading = false
      _that.form = Object.assign({}, form)
      _that.basePaperName = ''
      _that.dialogTitle = '增加担保人档案'
      _that.dialogStatus = '1'
      _that.dialogVisible = true
    },
    // 增加担保人档案
    addBtn() {
      const _that = this

      _that.clickLoading = true
      if (!_that.form.idPerson) {
        _that.$message.error('请选择担保人')
        return
      }
      var info = JSON.stringify(_that.form)
      var data = {
        'guarantorInfo': info
      }
      // console.log(data);
      // return

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

        _that.clickLoading = false
        _that.$message.error(err)
        _that.dialogVisible = true
      })
    },

    // 唤起修改
    handleUpdate(row) {
      const _that = this
      _that.form = Object.assign({}, form)
      _that.basePaperName = ''
      _that.dialogTitle = '修改担保人档案'
      _that.dialogVisible = true
      _that.dialogStatus = '2'

      var idPrint = row.id
      var data = {
        id: idPrint
      }

      getGuarantorInfo(data).then(res => {
        console.log(res)
        const data = res.data

        _that.form = {
          'id': data.id,
          'idPerson': data.idPerson, // 人员id,从人员档案选取,
          'memo': data.memo, // "担保说明",
          'moneyLimit': data.moneyLimit, // 担保金额,
          'personName': data.personName // 人员名称,
        }
      }).catch(err => {
        _that.$message.error(err)
      })
    },
    // 修改担保人档案
    editBtn() {
      const _that = this
      if (!_that.form.idPerson) {
        _that.$message.error('请选择担保人')
        return
      }
      var info = JSON.stringify(_that.form)
      var data = {
        'guarantorInfo': info
      }

      updateGuarantorInfo(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 printerTypeInfo = {
        id: id,
        voidFlag: status
      }
      var params = {
        'guarantorInfo': JSON.stringify(printerTypeInfo)
      }

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

    // 搜索
    searchData() {
      this.listData.page = 1
      this.getPaperInfo()
    },
    openChoose() {
      var _that = this
      _that.userVisible = true
    },
    chooseUser(row) {
      console.log(row)
      var _that = this
      _that.userVisible = false
      _that.form.idPerson = row.id
      _that.form.personName = row.userName
    },
    // 弹窗内选择操作
    chooseThis(row) {
      // console.log('点击选择');
      this.$emit('choosePaper', row)
    },
    // 查询统计选择
    chooseThisTo(row) {
      var data = {
        id: row.id,
        name: row.personName
      }
      this.$emit('chooseRow', data)
    },
    ERPChoose(row) {
      // this.$message.error(row)
      dyERP.sendJson(JSON.stringify(row))
    }
  }
}
</script>

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