<template>
  <div class="app-container">
    <div v-if="showPage == 1">
      <el-radio-group v-model="deduction" size="mini" @change="handleRadio">
        <el-radio-button label="全部" />
        <el-radio-button label="已退押" />
      </el-radio-group>
      <br /><br />
      <!-- 筛选条件 -->
      <div>
        <el-form ref="screenForm" :model="screenForm" label-width="120px" size="mini" label-position="left">
          <el-row :gutter="20">
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="工程登录编号" prop="refEnginRecordNo">
                <el-input v-model="screenForm.refEnginRecordNo" placeholder="请输入" />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="经销商编码/名称" prop="customerKeyword">
                <el-input v-model="screenForm.customerKeyword" placeholder="请输入" />
              </el-form-item>
            </el-col>
            <!-- <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="经销商名称" prop="customerKeyword">
                <el-input
                  v-model="screenForm.customerKeyword"
                  placeholder="请输入"
                ></el-input>
              </el-form-item>
            </el-col> -->
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="工程名称" prop="refProjectName">
                <el-input v-model="screenForm.refProjectName" placeholder="请输入" />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="发货申请日期" prop="startDeliverTime">
                <el-date-picker
                  v-model="screenForm.startDeliverTime"
                  class="dateStyle"
                  type="datetime"
                  placeholder="选择日期"
                />
              </el-form-item>
            </el-col>
            <!-- <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="产品名称" prop="">
                <el-input placeholder="请输入"></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="规格型号" prop="">
                <el-input placeholder="请输入"></el-input>
              </el-form-item>
            </el-col> -->
            <el-col :xs="24" :sm="24" :lg="24">
              <el-form-item label="" class="fr">
                <el-button size="mini" @click="resetScreenForm">清空</el-button>
                <el-button size="mini" type="primary" @click="submitScreenForm">搜索</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <!-- 按钮 -->
      <div class="btn-group clearfix" style="display: flex">
        <el-button type="primary" size="mini" @click="hanleDownloadFiles">导出</el-button>
        <el-upload
          v-if="$checkBtnRole('import', $route.meta.roles)"
          class="import-btn"
          :action="baseURL + 'student/import'"
          :http-request="handleImport"
          :file-list="importFileList"
          :show-file-list="false"
        >
          <el-button size="mini">导入</el-button>
        </el-upload>
      </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
            show-summary
            :summary-method="$getSummaries"
            @select-all="handleSelectionAllChange"
            @selection-change="handleSelectionAllChange"
          >
            <el-table-column align="left" type="selection" width="55" />
            <el-table-column
              align="left"
              label="工程登录编号"
              prop="refEnginRecordNo"
              min-width="160"
              show-overflow-tooltip
            />
            <el-table-column align="left" label="使用单位" prop="refUseUnit" min-width="160" show-overflow-tooltip />
            <el-table-column
              align="left"
              label="经销商编码"
              prop="customerNumber"
              min-width="160"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <CopyButton :copy-text="scope.row.customerNumber" />
                <span>{{ scope.row.customerNumber }}</span>
              </template>
            </el-table-column>
            <el-table-column align="left" label="经销商名称" prop="customerName" min-width="160" show-overflow-tooltip>
              <template slot-scope="scope">
                <CopyButton :copy-text="scope.row.customerName" />
                <span>{{ scope.row.customerName }}</span>
              </template>
            </el-table-column>
            <!-- <el-table-column
              align="left"
              label="是否免扣"
              prop=""
              min-width="160"
              show-overflow-tooltip
            ></el-table-column> -->
            <el-table-column align="left" label="是否退押" prop="isRefundDeposit" min-width="160" show-overflow-tooltip>
              <template slot-scope="scope">
                <el-tag v-if="scope.row.isRefundDeposit" type="success" size="small">是</el-tag>
                <el-tag v-else type="warning" size="small">否</el-tag>
              </template>
            </el-table-column>
            <!-- <el-table-column
              align="left"
              label="押金比例"
              prop=""
              min-width="160"
              show-overflow-tooltip
            ></el-table-column> -->
            <el-table-column align="right" label="押金金额" prop="depositAmount" min-width="160" show-overflow-tooltip>
              <template slot-scope="scope">
                {{ scope.row.depositAmount | numToFixed }}
              </template>
            </el-table-column>

            <el-table-column align="left" label="状态" prop="examineStatus" min-width="160" show-overflow-tooltip>
              <template slot-scope="scope">
                <el-tag v-if="scope.row.examineStatus == 'SAVE'" type="success" size="small">保存 </el-tag>
                <el-tag v-if="scope.row.examineStatus == 'WAIT'" type="warning" size="small">待审核 </el-tag>
                <el-tag v-if="scope.row.examineStatus == 'OK'" type="warning" size="mini">通过</el-tag>
                <el-tag v-if="scope.row.examineStatus == 'FAIL'" type="warning" size="small">不通过</el-tag>
                <el-tag v-if="scope.row.examineStatus == 'CLOSE'" type="warning" size="small">关闭</el-tag>
              </template>
            </el-table-column>
            <el-table-column align="right" label="订单数量" prop="qty" min-width="160" show-overflow-tooltip />
            <!-- <el-table-column
              align="left"
              label="是否已发货"
              prop=""
              min-width="160"
              show-overflow-tooltip
            ></el-table-column> -->
            <el-table-column align="center" label="操作" min-width="160" fixed="right" show-overflow-tooltip>
              <template slot-scope="scope">
                <el-button type="text" class="textColor" size="mini" @click="detailFn(scope.row)">详情</el-button>
                <el-button
                  v-if="scope.row.examineStatus == 'SAVE' && $checkBtnRole('apply', $route.meta.roles)"
                  type="text"
                  class="textColor"
                  @click="surrenderFn(scope.row)"
                  >申请退押</el-button
                >
                <!-- <el-button type="text" class="textColor" slot="reference"
                  >免扣退押</el-button
                > -->
              </template>
            </el-table-column>
          </el-table>
        </div>
        <!-- 分页 -->
        <div class="fr">
          <el-pagination
            :current-page="currentPage"
            :page-sizes="[10, 20, 30, 50]"
            :page-size="10"
            layout="total, sizes, prev, pager, next, jumper"
            :total="listTotal"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
    <DepositListDetail v-else-if="showPage == 2" />
    <DepositApplyDeduction v-else-if="showPage == 3" />
    <DepositApplySurrender v-else-if="showPage == 4" />
  </div>
</template>

<script>
import DepositListDetail from './components/deposit_list-detail'
import DepositApplyDeduction from './components/deposit-apply-deduction.vue'
import DepositApplySurrender from './components/deposit-apply-surrender.vue'
import { downloadFiles, handleImport } from '@/utils/util'
import { getList } from '@/api/engin_deposit.js'
import Mixin from '@/mixin'

export default {
  components: {
    DepositListDetail,
    DepositApplyDeduction,
    DepositApplySurrender
  },
  mixins: [Mixin],
  data() {
    return {
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      dataList: [], // 列表数据
      screenForm: {
        confirmName: '', // 审核人
        createName: '', // 创建人
        customerKeyword: '', // 客户编码/客户名称
        endDeliverTime: '', // 发货申请日期-结束
        enginOrderNo: '', // 工程订单编号
        enginOrderType: '', // 工程订单类型
        examineStatus: '', // 状态
        refEnginRecordNo: '', // 登录单号
        refProjectName: '', // 项目名称
        refUseUnit: '', // 使用单位
        startDeliverTime: '' // 发货申请日期-开始
      }, // 搜索表单
      listLoading: false, // 列表加载loading
      deduction: '全部',
      showPage: 1,
      importFileList: [],
      selectData: [],
      baseURL: ''
    }
  },

  methods: {
    // 免扣申请
    deductionFn() {
      this.showPage = 3
    },
    // 申请退押
    surrenderFn(row) {
      this.depositManageId = row.depositManageId
      this.showPage = 4
    },
    detailFn(row) {
      this.depositManageId = row.depositManageId
      this.refEnginRecordNo = row.refEnginRecordNo
      this.showPage = 2
    },
    getList() {
      this.listLoading = true
      const params = {
        pageSize: this.pageSize,
        pageNum: this.currentPage,
        isRefundDeposit: this.deduction === '已退押' ? true : null,
        ...this.screenForm
      }

      getList(params).then(res => {
        this.dataList = res.data.records
        res.data.records.forEach(item => {
          // item.number = (item.qty*100 - item.directTransferQty*100 - item.retiredQty*100) / 100;
          item.sums1 = ['qty']
          item.sums2 = ['depositAmount', 'diffAmount', 'price']
        })
        this.listLoading = false
        this.listTotal = res.data.total
      })
    },
    handleRadio() {
      this.getList()
    },
    hanleDownloadFiles() {
      if (this.selectData.length) {
        const arr = []
        for (let i = 0; i < this.selectData.length; i++) {
          arr.push(this.selectData[i].depositManageId)
        }
        downloadFiles('deposit-manage/export', { id: arr })
      } else {
        this.$errorMsg('请选择押金项')
      }
    },
    // 导入
    async handleImport(param) {
      this.importLoading = true
      const file = param.file
      console.log(file, 123)
      const formData = new FormData()
      formData.append('file', file)
      // formData.append("policyId", this.screenForm.code);
      const result = await handleImport('deposit-manage/import', formData)
      this.importLoading = false
      this.importFileList = []
      if (result.code == 200) {
        this.$alert(result.message, '导入成功', {
          confirmButtonText: '确定'
        })

        this.handletwoList()
      } else {
        this.$alert(result.message, '导入失败', {
          confirmButtonText: '确定'
        })
      }
    },
    handleSelectionAllChange(e) {
      this.selectData = e
    }
  }
}
</script>

<style lang="scss" scoped>
.dateStyle {
  width: 100%;
}
.import-btn {
  margin-left: 10px;
}
</style>
