<template>
  <div class="app-container">
    <div v-if="showSurrender">
      <el-radio-group @change="changeRadioFn" v-model="examine" size="mini">
        <el-radio-button label="">全部</el-radio-button>
        <el-radio-button label="SAVE">保存</el-radio-button>
        <el-radio-button label="WAIT">待审核</el-radio-button>
        <!-- <el-radio-button label="FAIL">审核驳回</el-radio-button> -->
        <el-radio-button label="OK">审核通过</el-radio-button>
      </el-radio-group>
      <br /><br />
      <!-- 筛选条件 -->
      <div>
        <el-form ref="searchForm" :model="searchForm" label-width="160px" 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="searchForm.refEnginRecordNo" placeholder="请输入"></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="经销商编码" prop="customerKeyword">
                <el-input v-model="searchForm.customerKeyword" placeholder="请输入"></el-input>
              </el-form-item>
            </el-col>
            <!-- <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="经销商名称" prop="customerKeyword">
                <el-input
                  v-model="searchForm.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="searchForm.refProjectName" placeholder="请输入"></el-input>
              </el-form-item>
            </el-col>
            <!-- <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="发货申请开始日期" prop="startDeliverTime">
                <el-date-picker
                  class="dateStyle"
                  v-model="searchForm.startDeliverTime"
                  type="datetime"
                  placeholder=""
                  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="endDeliverTime">
                <el-date-picker
                  v-model="searchForm.endDeliverTime"
                  class="dateStyle"
                  type="datetime"
                  placeholder=""
                  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="">
                <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="12" :lg="6">
              <el-form-item label="" class="fr">
                <el-button size="mini" @click="resetFn">清空</el-button>
                <el-button size="mini" type="primary" @click="searchFn">搜索</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <!-- 按钮 -->
      <div class="btn-group clearfix">
        <div class="fl">
          <el-radio-group @change="changeEnginOrderTypeFn" v-model="enginOrderType" size="mini">
            <el-radio-button label="HOME">家用工程</el-radio-button>
            <el-radio-button label="TRADE">商用工程</el-radio-button>
          </el-radio-group>
        </div>
        <div class="fr">
          <el-button type="primary" size="mini" @click="exportFn">导出</el-button>
          <el-upload
            class="import-btn"
            action=""
            :http-request="handleImport"
            :file-list="importFileList"
            :show-file-list="false"
          >
            <el-button type="primary" size="mini">导入</el-button>
          </el-upload>
        </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="handleSelectionChange"
            show-summary
            :summary-method="$getSummaries"
          >
            >
            <el-table-column type="selection" align="center" width="55"> </el-table-column>
            <el-table-column align="left" label="状态" prop="" min-width="160" show-overflow-tooltip>
              <template v-slot="{ row }">
                <el-tag size="mini" v-show="row.examineStatus == 'SAVE'">保存</el-tag>
                <el-tag size="mini" v-show="row.examineStatus == 'WAIT'" type="warning">待审核</el-tag>
                <el-tag size="mini" v-show="row.examineStatus == 'OK'" type="success">通过</el-tag>
                <el-tag size="mini" v-show="row.examineStatus == 'FAIL'" type="danger">不通过</el-tag>
              </template>
            </el-table-column>
            <el-table-column
              align="left"
              label="工程登录编号"
              prop="refEnginRecordNo"
              min-width="160"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <CopyButton :copyText="scope.row.refEnginRecordNo" />
                <span>{{ scope.row.refEnginRecordNo }}</span>
              </template>
            </el-table-column>
            <el-table-column align="left" label="项目类别" prop="refProjectName" min-width="160" show-overflow-tooltip>
              <template slot-scope="scope">
                <CopyButton :copyText="scope.row.refProjectName" />
                <span>{{ scope.row.refProjectName }}</span>
              </template>
            </el-table-column>

            <el-table-column
              align="left"
              label="经销商编码"
              prop="customerNumber"
              min-width="160"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <CopyButton :copyText="scope.row.customerNumber" />
                <span>{{ scope.row.customerNumber }}</span>
              </template>
            </el-table-column>
            <el-table-column align="left" label="经销商名称" prop="customerName" min-width="260" show-overflow-tooltip>
              <template slot-scope="scope">
                <CopyButton :copyText="scope.row.customerName" />
                <span>{{ scope.row.customerName }}</span>
              </template>
            </el-table-column>
            <el-table-column align="left" label="跨区厂编号" prop="factoryNo" min-width="260" show-overflow-tooltip>
              <template slot-scope="scope">
                <CopyButton :copyText="scope.row.factoryNo" />
                <span>{{ scope.row.factoryNo }}</span>
              </template>
            </el-table-column>
            <el-table-column
              align="left"
              label="产品编码"
              prop="materialOldNumber"
              min-width="260"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <CopyButton :copyText="scope.row.materialOldNumber" />
                <span>{{ scope.row.materialOldNumber }}</span>
              </template>
            </el-table-column>
            <el-table-column align="left" label="物料编码" prop="materialNumber" min-width="260" show-overflow-tooltip>
              <template slot-scope="scope">
                <CopyButton :copyText="scope.row.materialNumber" />
                <span>{{ scope.row.materialNumber }}</span>
              </template>
            </el-table-column>
            <el-table-column align="left" label="产品名称" prop="materialName" min-width="260" show-overflow-tooltip>
              <template slot-scope="scope">
                <CopyButton :copyText="scope.row.materialName" />
                <span>{{ scope.row.materialName }}</span>
              </template>
            </el-table-column>
            <el-table-column align="left" label="规格型号" prop="specification" min-width="260" show-overflow-tooltip>
              <template slot-scope="scope">
                <CopyButton :copyText="scope.row.specification" />
                <span>{{ scope.row.specification }}</span>
              </template>
            </el-table-column>
            <el-table-column
              align="right"
              label="押金金额"
              prop="itemDepositAmount"
              min-width="160"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                {{ scope.row.itemDepositAmount | numToFixed }}
              </template>
            </el-table-column>
            <el-table-column fixed="right" align="center" label="操作" min-width="160" show-overflow-tooltip>
              <template slot-scope="scope">
                <el-button
                  type="text"
                  class="textColor"
                  v-if="scope.row.examineStatus == 'WAIT' && $checkBtnRole('examine', $route.meta.roles)"
                  @click="examineFn(scope.row.depositManageId)"
                  >审批</el-button
                >

                <el-button
                  type="text"
                  class="textColor"
                  v-if="
                    scope.row.examineStatus == 'SAVE' ||
                    scope.row.examineStatus == 'OK' ||
                    scope.row.examineStatus == 'FAIL'
                  "
                  @click="infoFn(scope.row.depositManageId)"
                  >详情</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>
    <RefundListDetail @updateList="updateList" :detailId="detailId" v-else />
  </div>
</template>

<script>
import { getDepositManageList, getDepositManagExport } from '@/api/engin_deposit/refund_list'
import { downloadFiles, handleImport } from '@/utils/util'
import RefundListDetail from './components/refund_list-detail.vue'
export default {
  components: {
    RefundListDetail
  },
  data() {
    return {
      importFileList: [],
      enginOrderType: 'HOME', //列表类型
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      dataList: [], // 列表数据
      searchForm: {
        refEnginRecordNo: '',
        customerKeyword: '',
        refProjectName: ''
        // startDeliverTime: "",
        // endDeliverTime: "",
      }, //搜索表单
      listLoading: false, // 列表加载loading
      examine: '',
      showSurrender: true,
      detailId: '',
      selectData: []
    }
  },
  created() {
    this.getDataList({
      pageSize: this.pageSize,
      pageNum: this.currentPage,
      confirmName: '',
      createName: '',
      customerKeyword: '',
      // endDeliverTime: "",
      enginOrderNo: '',
      enginOrderType: this.enginOrderType,
      examineStatus: '',
      refEnginRecordNo: '',
      refProjectName: '',
      refUseUnit: ''
      // startDeliverTime: "",
    })
  },
  methods: {
    // 导入
    async handleImport(param) {
      const file = param.file

      const formData = new FormData()
      formData.append('file', file)

      let result = await handleImport('deposit-manage/import', formData)

      console.log(result)
      this.importFileList = []
      if (result.code == 200) {
        this.$message.success('导入成功')
        this.getDataList({
          pageSize: this.pageSize,
          pageNum: this.currentPage,
          confirmName: '',
          createName: '',
          customerKeyword: '',
          // endDeliverTime: "",
          enginOrderNo: '',
          enginOrderType: this.enginOrderType,
          examineStatus: '',
          refEnginRecordNo: '',
          refProjectName: '',
          refUseUnit: ''
          // startDeliverTime: "",
        })
      } else {
        this.$message.error(result.message)
      }
    },
    //审批后更新列表数据
    updateList() {
      this.getDataList({
        pageSize: this.pageSize,
        pageNum: this.currentPage,
        confirmName: '',
        createName: '',
        customerKeyword: '',
        // endDeliverTime: "",
        enginOrderNo: '',
        enginOrderType: this.enginOrderType,
        examineStatus: '',
        refEnginRecordNo: '',
        refProjectName: '',
        refUseUnit: ''
        // startDeliverTime: "",
      })
    },
    //切换列表
    changeEnginOrderTypeFn(v) {
      // console.log(v);
      this.pageSize = 10
      this.currentPage = 1

      this.getDataList({
        pageSize: this.pageSize,
        pageNum: this.currentPage,
        confirmName: '',
        createName: '',
        customerKeyword: '',
        // endDeliverTime: "",
        enginOrderNo: '',
        enginOrderType: v,
        examineStatus: this.examine,
        refEnginRecordNo: '',
        refProjectName: '',
        refUseUnit: ''
        // startDeliverTime: "",
      })
    },
    // 更改每页数量
    handleSizeChange(val) {
      this.pageSize = val
      this.currentPage = 1
      this.getDataList({
        pageSize: this.pageSize,
        pageNum: this.currentPage,
        confirmName: '',
        createName: '',
        customerKeyword: '',
        // endDeliverTime: "",
        enginOrderNo: '',
        enginOrderType: this.enginOrderType,
        examineStatus: this.examine,
        refEnginRecordNo: '',
        refProjectName: '',
        refUseUnit: ''
        // startDeliverTime: "",
      })
    },
    // 更改当前页
    handleCurrentChange(val) {
      this.currentPage = val
      this.getDataList({
        pageSize: this.pageSize,
        pageNum: this.currentPage,
        confirmName: '',
        createName: '',
        customerKeyword: '',
        // endDeliverTime: "",
        enginOrderNo: '',
        enginOrderType: this.enginOrderType,
        examineStatus: this.examine,
        refEnginRecordNo: '',
        refProjectName: '',
        refUseUnit: ''
        // startDeliverTime: "",
      })
    },
    //切换radio
    changeRadioFn(v) {
      this.examine = v
      this.getDataList({
        pageSize: this.pageSize,
        pageNum: this.currentPage,
        confirmName: '',
        createName: '',
        customerKeyword: '',
        // endDeliverTime: "",
        enginOrderNo: '',
        enginOrderType: this.enginOrderType,
        examineStatus: this.examine,
        refEnginRecordNo: '',
        refProjectName: '',
        refUseUnit: ''
        // startDeliverTime: "",
      })
    },
    //导出
    exportFn() {
      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('请选择押金项')
      }
    },
    //多选列表数据
    handleSelectionChange(selectData) {
      this.selectData = selectData
    },
    //重置
    resetFn() {
      this.$refs.searchForm.resetFields()
    },
    //搜索
    searchFn() {
      this.getDataList({
        pageSize: this.pageSize,
        pageNum: this.currentPage,
        confirmName: '',
        createName: '',
        enginOrderNo: '',
        enginOrderType: this.enginOrderType,
        examineStatus: this.examine,
        refUseUnit: '',
        ...this.searchForm
      })
    },
    //详情
    infoFn(id) {
      this.detailId = id
      this.showSurrender = false
    },
    async getDataList(data) {
      const res = await getDepositManageList(data)
      res.data.records.forEach(item => {
        item.sums1 = []
        item.sums2 = ['itemDepositAmount']
      })
      this.dataList = res.data.records
      this.listTotal = res.data.total
    },
    examineFn(id) {
      this.detailId = id
      this.showSurrender = false
    }
  }
}
</script>

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