<template>
  <div class="app-container">
    <div class="filter-container">
      <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="getInfo"/>
        </el-tooltip>
      </div>
    </div>
    <el-table
      :data="tableData"
      v-loading="listLoading"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      :tree-props="{child: 'child', haschild: 'child'}">
      <el-table-column
        prop="accountName"
        label="科目名称">
      </el-table-column>

      <el-table-column
        prop="accountCode"
        label="科目编码">
      </el-table-column>
      <el-table-column
        prop="bankFlag"
        label="银行科目">
        <template slot-scope="{row}">
        <span v-if="row.bankFlag==0">
          否
        </span>
          <span v-else-if="row.bankFlag==1">
          是
        </span>
        </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-if="row.voidFlag==1" type="danger">
            禁用
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column  label="操作" fixed="left" align="center" 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 v-if="row.levelNo ==1" @click="handleCreate(row)" size="mini" type="success">
            新增
          </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>
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="500px">
      <el-form label-width="130px" label-position="right">
        <el-form-item label="科目名称">
          <el-input v-model="form.accountName"></el-input>
        </el-form-item>
        <el-form-item label="科目编码">
          <el-input v-model="form.accountCode"></el-input>
        </el-form-item>
        <el-form-item label="银行科目">
          <el-radio v-model="form.bankFlag" :label="1">是</el-radio>
          <el-radio v-model="form.bankFlag" :label="0">否</el-radio>
        </el-form-item>
        <el-form-item label="是否连号">
          <el-radio v-model="form.receiptsFollowFlag" :label="1">是</el-radio>
          <el-radio v-model="form.receiptsFollowFlag" :label="0">否</el-radio>
        </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 { getAccountInfoList, addAccountInfo, getAccountInfoById, updateAccountInfo } from '@/api/baseInfo/subjectInfo.js'
import Cancledialog from '@/components/Dialog/Cancledialog.vue'
const form = {
  accountName: '',
  accountCode: '',
  bankFlag: 0,
  receiptsFollowFlag: 0
}
export default {
  components: {
    Cancledialog
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        1: 'success',
        0: 'danger'
      }
      return statusMap[status]
    }
  },
  props: {
    isChoose: {
      type: Boolean,
      default: false
    },
    /**
     * 调用来源
     * 0 普通调用
     * 1 查询统计调用
     */
    sourceType: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      webType: true,
      dialogStatus: 1, // 1增加，2修改
      dialogTitle: '',
      dialogVisible: false,
      innerVisible: false,
      confirmdialog: false,
      listLoading: false,
      nameTitle: '',
      createType: 1, // 1增加一级科目，1增加二级科目

      handleTree: false,
      defaultProps: {
        label: 'accName'
      },
      checkName: '',

      form: Object.assign({},form),

      tableData: [],

      listData: {
        total: 0,
        page: 1,
        pageSize: 10
      },
      menuId: ''
    }
  },
  mounted() {
    this.getInfo()
    if (this.$route.query.type === undefined) {
      this.webType = true
    } else {
      this.webType = false
    }
  },
  methods: {
    // 获取科目档案列表
    getInfo() {
      const _that = this
      _that.listLoading = true
      var params = {
        // levNo: 1
      }
      var page = {
        pageIndex: _that.listData.page,
        pageSize: _that.listData.pageSize
      }
      var data = {
        'params': JSON.stringify(params),
        'page': JSON.stringify(page)
      }
      // console.log(data);
      getAccountInfoList(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
      })
    },

    // 唤起增加一级科目档案
    handleCreateAdd() {
      const _that = this
      _that.form = Object.assign({},form)
      // _that.form = {
      //   accName: '',
      //   parentEncode: '',
      //   levelNo: '1'
      // }
      _that.dialogTitle = '增加科目'
      _that.dialogStatus = '1'
      _that.dialogVisible = true
      _that.createType = 1
    },

    // 增加二级科目
    handleCreate(row) {
      const _that = this

      _that.form = {
        accName: '',
        parentEncode: '',
        levelNo: '2'
      }
      _that.dialogStatus = '1'
      _that.createType = 2
      _that.dialogTitle = '增加二级科目'
      _that.dialogVisible = true
      console.log(row)
      _that.nameTitle = row.accName
      _that.form.parentEncode = row.accEncode
    },

    // 增加科目档案
    addBtn() {
      const _that = this

      var depotInfo = JSON.stringify(_that.form)

      var data = {
        'financeAccount': depotInfo
      }
      // console.log(data);
      // return

      addAccountInfo(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.getInfo()
    },

    // 唤起修改
    handleUpdate(row) {
      const _that = this
      _that.form = Object.assign({}, form)

      _that.dialogTitle = '修改科目'
      _that.dialogVisible = true
      _that.dialogStatus = '2'
      var idPrint = row.id
      var data = {
        accId: idPrint
      }

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

        _that.form = {
          id: data.id,
          accountName: data.accountName,
          accountCode: data.accountCode,
          bankFlag: data.bankFlag,
          receiptsFollowFlag: data.receiptsFollowFlag
        }
      }).catch(err => {
        _that.$message.error(err)
      })
    },

    // 修改科目
    editBtn() {
      const _that = this
      var infoType = JSON.stringify(_that.form)
      var data = {
        'financeAccount': infoType
      }

      // console.log(data);
      // return
      updateAccountInfo(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 = {
        'financeAccount': JSON.stringify(printerTypeInfo)
      }

      var url = '/finance/voidFinanceAccount'
      _that.$refs.confirmDialog.updataClick(params, url)
    },
    // 弹窗内选择操作
    chooseThis(row) {
      // console.log('点击选择');
      this.$emit('choosePaper', row)
    },

    // 查询统计选择
    chooseThisTo(row) {
      var data = {
        id: row.id,
        name: row.accountName
      }
      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>
