<template>
  <div class="sales-contract-management">
    <router-view />
    <template v-if="!$route.params.id">
      <div class="search">
        <div class="left">
          <div>
            <el-button v-power="'saveFinancial'" type="primary" @click="modifyClick('create')">新增出入账</el-button>
          </div>
        </div>
        <div class="right">
          <div>
            <el-select
              v-model="search.cashStatus"
              placeholder="请选择状态"
              clearable
              style="width: 100%"
            >
              <el-option label="已创建" :value="1"></el-option>
              <el-option label="已分配" :value="2"></el-option>
              <el-option label="待处理" :value="3"></el-option>
              <el-option label="已完成" :value="4"></el-option>
            </el-select>
          </div>
          <div>
            <el-input v-model="search.fastSearch" style="width: 300px" placeholder="请输入项目名称/主企业名称/核心企业名称" clearable></el-input>
          </div>
          <div class="leftBtn">
            <el-button type="primary" @click="fetchData('search')">查询</el-button>
          </div>
        </div>
      </div>
      <cy-comm-table
        ref="commTable"
        v-loading="loading"
        :columns="columns"
        :data="tableData"
        :count="total"
        :table-options="tableOptions"
      />
    </template>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import indexTable from './indexTable'
import { getList, delteRowData } from '@/api/financialManagement/financialManagement'
export default {
  name: 'Demo',
  mixins: [indexTable],
  data() {
    return {
      loading: false,
      activeName: 'pending',
      tableData: [],
      page: 1,
      size: 10,
      total: 0,
      search: {
        cashStatus: '',
        fastSearch: ''
      }
    }
  },
  computed: {
    ...mapGetters([
      'name'
    ])
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    fetchData(type) {
      if (type === 'search') {
        this.page = 1
        this.size = 10
      }

      this.loading = true
      const params = {
        ...this.search,
        page: this.page || 1,
        rows: this.size || 10
      }
      getList(params).then(response => {
        this.tableData = response.rows
        this.loading = false
        this.total = response.records
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!')
        } else {
          return false
        }
      })
    },
    modifyClick() {
      this.$router.push({
        name: 'FinancialManagementAddAccounts',
        params: { id: 'children' }
      })
    },
    handlerOperate(type, row, msg) {
      if (type === 'delete') {
        this.$confirm('是否删除该条记录', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delteRowData(row.id).then(() => {
            this.fetchData()
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          })
        })
        return
      }
      if (row.cashStatus === '1') {
        this.$router.push({
          name: 'FinancialManagementAddAccounts',
          params: { id: 'children' },
          query: {
            taskName: row.taskName,
            type,
            id: row.id,
            msg,
            entryOutType: row.entryOutType
          }
        })
      } else if (row.taskName === '财务审核') {
        this.$router.push({
          name: 'InvoicesManagementFinancialReview',
          params: { id: 'children' }
        })
      } else {
        this.$router.push({
          name: 'FinancialManagementView',
          params: { id: 'children' },
          query: {
            taskName: row.taskName,
            type,
            id: row.id,
            entryOutType: row.entryOutType // 出入账类型 1--入账 2--出账
          }
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.sales-contract-management {

}
</style>
