<template>
  <div class="app-container">
    <div v-if="showPage == 1">
      <!-- 筛选条件 -->
      <div>
        <Collapse :screen-form="searchForm">
          <template #right_btn>
            <el-button size="mini" @click="clearFn">清空</el-button>
            <el-button size="mini" type="primary" @click="searchFn">搜索</el-button>
          </template>
          <template #left_btn>
            <el-radio-group @change="changeRadioGroupFn" v-model="examine" size="mini">
              <el-radio-button label="">全部</el-radio-button>
              <el-radio-button label="WAIT">待审核</el-radio-button>
              <el-radio-button label="OK">审核通过</el-radio-button>
              <!-- <el-radio-button label="FAIL">审核驳回</el-radio-button> -->
            </el-radio-group>
          </template>
          <template #search>
            <el-form ref="searchForm" :model="searchForm" label-width="100px" size="mini" label-position="left">
              <el-row :gutter="20">
                <el-col :xs="24" :sm="12" :lg="6">
                  <el-form-item label="单据来源" prop="source">
                    <el-select v-model="searchForm.source" class="selectStyle" placeholder="请选择" filterable>
                      <el-option value="工程押金"> </el-option>
                      <el-option value="保证金"> </el-option>
                      <el-option value="工程价差"> </el-option>
                      <el-option value="仓储费"> </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :lg="6">
                  <el-form-item label="单据编号" prop="code">
                    <el-input v-model="searchForm.code" placeholder="请输入"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :lg="6">
                  <el-form-item label="往来单位" prop="userName">
                    <!-- <el-input v-model="searchForm.userName" placeholder="请输入"></el-input> -->
                    <el-select class="selectStyle" v-model="searchForm.userName" placeholder="请选择" filterable>
                      <el-option v-for="(v, i) in customerList" :key="i" :label="v.name" :value="v.name"> </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :lg="6">
                  <el-form-item label="业务开始时间" prop="startTime">
                    <el-date-picker
                      class="dateStyle"
                      v-model="searchForm.startTime"
                      placeholder="选择日期"
                      type="datetime"
                      default-time="00:00:00"
                      value-format="yyyy-MM-dd HH:mm:ss"
                    >
                    </el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :lg="6">
                  <el-form-item label="业务结束时间" prop="endTime">
                    <el-date-picker
                      class="dateStyle"
                      v-model="searchForm.endTime"
                      placeholder="选择日期"
                      type="datetime"
                      default-time="23:59:59"
                      value-format="yyyy-MM-dd HH:mm:ss"
                    >
                    </el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :lg="6">
                  <el-form-item label="审核人" prop="confirmName">
                    <el-input v-model="searchForm.confirmName" placeholder="请输入审核人" />
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </template>
        </Collapse>
      </div>
      <!-- 按钮 -->
      <div class="btn-group clearfix">
        <div class="fl">
          <el-button
            v-if="$checkBtnRole('add', $route.meta.roles)"
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="addFn"
            >新建</el-button
          >

          <el-popconfirm
            v-if="$checkBtnRole('del', $route.meta.roles)"
            class="delClass"
            @onConfirm="deleFn"
            title="这是一段内容确定删除吗？"
          >
            <el-button :disabled="deleList.length < 1" slot="reference" type="danger" icon="el-icon-minus" size="mini"
              >批量删除</el-button
            >
          </el-popconfirm>
        </div>
        <div class="fr">
          <ExportButton :exUrl="'finance/other/rece/listExport'" :exParams="exParams" />
        </div>
      </div>
      <!-- 列表 -->
      <div class="mymain-container">
        <div class="table">
          <el-table
            v-loading="listLoading"
            :data="dataList"
            element-loading-text="Loading"
            border
            fit
            highlight-current-row
            stripe
            @selection-change="selectionChangeFn"
            show-summary
            :summary-method="$getSummaries"
          >
            <el-table-column align="center" type="selection" width="51"> </el-table-column>
            <el-table-column
              align="left"
              label="单据类型"
              prop="billType"
              min-width="100"
              show-overflow-tooltip
            ></el-table-column>
            <el-table-column
              align="left"
              label="单据来源"
              prop="source"
              min-width="100"
              show-overflow-tooltip
            ></el-table-column>
            <el-table-column align="left" label="单据编码" prop="code" min-width="190" show-overflow-tooltip>
              <template slot-scope="scope">
                <CopyButton :copyText="scope.row.code" />
                <span>{{ scope.row.code }}</span>
              </template>
            </el-table-column>
            <el-table-column
              align="left"
              label="业务日期"
              prop="theTime"
              min-width="160"
              show-overflow-tooltip
            ></el-table-column>
            <el-table-column align="left" label="往来单位类型" prop="userType" min-width="140" show-overflow-tooltip>
              <template slot-scope="scope">
                {{ scope.row.userType == 'BD_Customer' ? '客户' : '' }}
              </template>
            </el-table-column>
            <el-table-column align="left" label="往来单位" prop="userName" min-width="260" show-overflow-tooltip>
              <template slot-scope="scope">
                <CopyButton :copyText="scope.row.userName" />
                <span>{{ scope.row.userName }}</span>
              </template>
            </el-table-column>
            <el-table-column
              align="left"
              label="币别"
              prop="amountType"
              min-width="100"
              show-overflow-tooltip
            ></el-table-column>

            <el-table-column align="left" label="单据状态" prop="examineStatus" min-width="160" show-overflow-tooltip>
              <template slot-scope="scope">
                <el-tag size="mini" v-show="scope.row.examineStatus == 'SAVE'">保存</el-tag>
                <el-tag size="mini" v-show="scope.row.examineStatus == 'WAIT'" type="warning">待审核</el-tag>
                <el-tag size="mini" v-show="scope.row.examineStatus == 'OK'" type="success">通过</el-tag>
                <el-tag size="mini" v-show="scope.row.examineStatus == 'FAIL'" type="danger">不通过</el-tag>
                <el-tag size="mini" v-show="scope.row.examineStatus == 'CLOSE'" type="info">已关闭</el-tag>
              </template>
            </el-table-column>
            <el-table-column align="right" label="总金额" prop="itemAmount" min-width="120" show-overflow-tooltip>
              <template slot-scope="scope">
                {{ scope.row.itemAmount | numToFixed }}
              </template>
            </el-table-column>
            <el-table-column
              align="left"
              label="项目费用名称"
              prop="projectName"
              min-width="160"
              show-overflow-tooltip
            ></el-table-column>
            <el-table-column
              align="left"
              label="审核人"
              prop="examineBy"
              min-width="160"
              show-overflow-tooltip
            ></el-table-column>
            <el-table-column
              align="left"
              label="审核时间"
              prop="examineTime"
              min-width="160"
              show-overflow-tooltip
            ></el-table-column>

            <el-table-column align="center" label="操作" min-width="160" show-overflow-tooltip fixed="right">
              <template slot-scope="scope">
                <el-button
                  v-if="
                    (scope.row.examineStatus == 'WAIT' || scope.row.examineStatus == 'FAIL') &&
                    $checkBtnRole('examine', $route.meta.roles)
                  "
                  type="text"
                  @click="approvalFn(scope.row.id)"
                  >审批</el-button
                >
                <el-button type="text" v-if="scope.row.examineStatus == 'SAVE'" @click="bringFn(scope.row.id)"
                  >提审</el-button
                >
                <el-button
                  type="text"
                  v-if="scope.row.examineStatus == 'OK' || scope.row.examineStatus == 'FAIL'"
                  @click="unApprovalFn(scope.row.id)"
                  >弃审</el-button
                >
                <el-button type="text" v-if="scope.row.examineStatus == 'SAVE'" @click="detailFn(scope.row.id, 'edit')"
                  >编辑</el-button
                >
                <el-button type="text" @click="detailFn(scope.row.id, 'detail')">详情</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <!-- 分页 -->
        <div class="fr">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[10, 20, 30, 50]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="listTotal"
          >
          </el-pagination>
        </div>
      </div>
    </div>
    <ReceivableListAdd @updateList="updateList" v-else-if="showPage == 2" />
    <ReceivableListApproval :approvalId="approvalId" @updateList="updateList" v-else-if="showPage == 3" />
    <ReceivableListDetail :approvalId="approvalId" :czType="czType" v-else-if="showPage == 4" />
  </div>
</template>

<script>
import { getCustomerList } from '@/api/finance/wallet'
import {
  getFinanceOtherReceList,
  getFinanceOtherReceDelete,
  getFinanceOtherReceApply,
  getFinanceOtherReceAbandon
} from '@/api/finance/receivable_list'
import ReceivableListAdd from './components/receivable_list-add'
import ReceivableListApproval from './components/receivable_list-approval'
import ReceivableListDetail from './components/receivable_list-detail'
export default {
  components: {
    ReceivableListAdd,
    ReceivableListApproval,
    ReceivableListDetail
  },
  data() {
    return {
      customerList: [],
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      dataList: [], // 列表数据
      searchForm: {
        source: '',
        code: '',
        userName: '',
        startTime: '',
        endTime: '',
        confirmName: ''
      }, //搜索表单
      listLoading: false, // 列表加载loading
      examine: '',
      showPage: 1,
      approvalId: null,
      czType: '',
      isCollapse: true,
      deleList: []
    }
  },
  computed: {
    exParams() {
      return {
        source: this.searchForm.source,
        code: this.searchForm.code,
        userName: this.searchForm.userName,
        startTime: this.searchForm.startTime,
        endTime: this.searchForm.endTime,
        examineStatus: this.examine
      }
    }
  },
  created() {
    this.getDataList({ pageSize: this.pageSize, pageNum: this.currentPage })
    this.getCustomerDataList()
  },
  methods: {
    //获取经销商列表
    async getCustomerDataList() {
      let res = await getCustomerList({
        pageNum: 1,
        pageSize: -1
      })
      this.customerList = res.data.records
    },
    //提审
    async bringFn(id) {
      await getFinanceOtherReceApply({ id })
      this.$message.success('提审成功')
      this.getDataList({
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        examineStatus: this.examine
      })
    },
    //弃审
    async unApprovalFn(id) {
      await getFinanceOtherReceAbandon({ id })
      this.$message.success('弃审成功')
      this.getDataList({
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        examineStatus: this.examine
      })
    },
    //radio切换'
    changeRadioGroupFn(v) {
      this.getDataList({
        pageSize: this.pageSize,
        pageNum: this.currentPage,
        examineStatus: v
      })
    },
    // 更改每页数量
    handleSizeChange(val) {
      this.pageSize = val
      this.currentPage = 1
      this.getDataList({
        pageNum: 1,
        pageSize: this.pageSize,
        examineStatus: this.examine
      })
    },
    // 更改当前页
    handleCurrentChange(val) {
      this.currentPage = val
      this.getDataList({
        pageNum: val,
        pageSize: 10,
        examineStatus: this.examine
      })
    },
    //清除
    async clearFn() {
      await this.$refs.searchForm.resetFields()
      this.examine = ''
    },
    //搜索
    searchFn() {
      this.getDataList({
        ...this.searchForm,
        // examineStatus: this.examine,
        pageSize: this.pageSize,
        pageNum: this.currentPage
      })
    },
    //删除
    async deleFn() {
      let res = this.deleList.toString()
      console.log(res)
      await getFinanceOtherReceDelete({ ids: res })
      this.getDataList({ pageSize: this.pageSize, pageNum: this.currentPage })
      this.$message.success('删除成功')
      this.deleList = []
    },
    selectionChangeFn(value) {
      // console.log(value);
      const res = value.map(v => v.itemId)
      // console.log(res);
      this.deleList = res
    },
    //新建后更新列表
    updateList() {
      this.getDataList({ pageSize: this.pageSize, pageNum: this.currentPage })
    },
    //获取来列表数据
    async getDataList(data) {
      let res = await getFinanceOtherReceList(data)
      // console.log(res);
      res.data.records.forEach(item => {
        item.sums2 = ['itemAmount']
        item.sums1 = ['itemAmount']
      })
      this.dataList = res.data.records
      this.listTotal = res.data.total
    },
    //详情
    detailFn(id, type) {
      this.czType = type
      this.approvalId = id
      this.showPage = 4
    },
    //审批
    approvalFn(id) {
      this.approvalId = id
      this.showPage = 3
    },
    //新建
    addFn() {
      this.showPage = 2
    }
  }
}
</script>
<style lang="scss" scoped>
.selectStyle {
  width: 100%;
}
.dateStyle {
  width: 100%;
}
.delClass {
  margin-left: 10px;
}
</style>
