<template>
  <ContainerQuery>
    <div slot="more">
      <el-form size="small" :inline="true">
        <el-form-item>
          <SelectSupplier
            v-model="searchDate.supplierId"
            @clear="clearSupplier"
            @change="selUnitSupplier"
          />
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="searchDate.settlement_type"
            clearable
            placeholder="提现方式"
            @change="pageChange(1)"
          >
            <el-option
              v-for="item in type_form"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="searchDate.time"
            clearable
            type="daterange"
            value-format="timestamp"
            range-separator="-"
            start-placeholder="申请开始日期"
            end-placeholder="申请结束日期"
            @change="settlementDate"
          ></el-date-picker>
        </el-form-item>
      </el-form>
    </div>

    <el-tabs v-model="activeName" type="card" @tab-click="tabChange">
      <el-tab-pane label="全部" name="0"></el-tab-pane>
      <el-tab-pane label="待审核" name="1"></el-tab-pane>
      <el-tab-pane label="待打款" name="4"></el-tab-pane>
      <el-tab-pane label="已结算" name="2"></el-tab-pane>
      <el-tab-pane label="已拒绝" name="3"></el-tab-pane>
    </el-tabs>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column
        prop="accountContent.name"
        label="姓名"
        min-width="100"
      ></el-table-column>
      <el-table-column
        prop="accountContent.account"
        label="账号"
        min-width="140"
      ></el-table-column>
      <el-table-column
        prop="supplierName"
        label="供应商"
        min-width="120"
      ></el-table-column>
      <el-table-column prop="money" label="提现金额" min-width="100">
        <template slot-scope="scope">
          {{ $_common.formattedNumber(scope.row.money) }}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="提现方式" min-width="100">
        <template slot-scope="scope">
          {{
            scope.row.type === 1
              ? "微信钱包"
              : scope.row.type === 2
              ? "支付宝提现"
              : scope.row.type === 3
              ? "银行打款"
              : "其他"
          }}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="提现状态" min-width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.auditStatus === 1" class="info-status">
            待审核
          </span>
          <span v-else-if="scope.row.auditStatus === 2" class="success-status">
            已结算
          </span>
          <el-tooltip
            v-else-if="scope.row.auditStatus === 3"
            effect="dark"
            :content="scope.row.reason"
            placement="top"
          >
            <span class="danger-status">已拒绝</span>
          </el-tooltip>
          <span v-else-if="scope.row.auditStatus === 4" class="primary-status">
            待打款
          </span>
          <span v-else>其他</span>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="申请时间" min-width="160">
        <template slot-scope="scope">
          {{ $_common.formatDate(scope.row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column prop="address" label="操作" min-width="140">
        <template slot-scope="scope">
          <el-button
            v-if="
              $accessCheck(
                $Access.SupplierSettlement_SupplierWithdrawalUpStatus
              )
            "
            type="text"
            size="mini"
            :disabled="
              scope.row.auditStatus === 2 ||
              scope.row.auditStatus === 3 ||
              scope.row.auditStatus === 4
            "
            @click="editSel(4, scope.row.id)"
          >
            审核
          </el-button>
          <el-button
            v-if="
              $accessCheck($Access.SupplierSettlement_SupplierWithdrawalRefulse)
            "
            type="text"
            size="mini"
            :disabled="
              scope.row.auditStatus === 2 ||
              scope.row.auditStatus === 3 ||
              scope.row.auditStatus === 4
            "
            @click="deleteSelt(scope.row.id)"
          >
            拒绝
          </el-button>
          <el-button
            v-if="
              $accessCheck($Access.SupplierSettlement_SupplierWithdrawalCash)
            "
            type="text"
            size="mini"
            :disabled="scope.row.auditStatus !== 4"
            @click="editSel(2, scope.row.id)"
          >
            打款
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <FooterPage
      :page-size="pageSize"
      :total-page.sync="total"
      :current-page.sync="page"
      @pageChange="pageChange"
      @sizeChange="sizeChange"
    />
    <el-dialog
      :close-on-press-escape="false"
      :close-on-click-modal="false"
      title="拒绝原因"
      :visible.sync="deleteSel"
      width="30%"
    >
      <span>
        <el-form
          ref="add_form"
          :rules="rules"
          label-width="100px"
          class="demo-ruleForm"
          :model="add_form"
        >
          <el-form-item label="拒绝原因" prop="delete_reason">
            <el-input
              v-model="add_form.delete_reason"
              placeholder="请输入拒绝原因"
              size="small"
            ></el-input>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteSel = false">取 消</el-button>
        <el-button type="primary" @click="deleteSon">确 定</el-button>
      </span>
    </el-dialog>
  </ContainerQuery>
</template>

<script>
  import { getAllSupplierWithdrawal, updateAuditStatus } from "@/api/Supplier";
  import SelectSupplier from "@/component/common/SelectSupplier";
  export default {
    name: "Settlement",
    components: {
      SelectSupplier,
    },
    data() {
      return {
        searchDate: {
          supplierId: "",
          settlement_type: "",
          settlement_status: "",
          time: [],
          start: "",
          end: "",
        },
        add_form: {
          delete_reason: "",
        },

        deleteSel: false,
        tableData: [{}],
        total: 0,
        page: 1,
        pageSize: 10,
        del_id: 0,
        rules: {
          delete_reason: [
            { required: true, message: "请输入拒绝原因", trigger: "blur" },
          ],
        },
        activeName: "0",
        type_form: [
          {
            value: 0,
            label: "全部",
          },
          {
            value: 1,
            label: "微信钱包",
          },
          {
            value: 2,
            label: "支付宝",
          },
          {
            value: 3,
            label: "银行卡",
          },
        ],
      };
    },
    created() {
      this.getAllSupplierWithdrawal();
    },
    methods: {
      tabChange() {
        this.searchDate.settlement_status = parseInt(this.activeName);
        this.pageChange(1);
      },
      pageChange(val) {
        this.page = val;
        this.getAllSupplierWithdrawal();
      },
      // 每页数据大小改变
      sizeChange(val) {
        this.pageSize = val;
        this.pageChange(1);
      },
      settlementDate(val) {
        if (val && val.length) {
          this.searchDate.start = val[0] / 1000;
          this.searchDate.end = val[1] / 1000 + 86399;
        } else {
          this.searchDate.start = "";
          this.searchDate.end = "";
        }
        this.pageChange(1);
      },
      async getAllSupplierWithdrawal() {
        const data = await getAllSupplierWithdrawal({
          page: this.page,
          pageSize: this.pageSize,
          auditStatus: this.searchDate.settlement_status,
          startTime: this.searchDate.start,
          endTime: this.searchDate.end,
          type: this.searchDate.settlement_type,
          supplierId: this.searchDate.supplierId,
        });
        this.tableData = data.data;
        this.total = data.pageTotal;
      },
      editSel(status, id) {
        const tip = status === 4 ? "通过审核" : "打款";
        this.$confirm(`确定要${tip}吗？`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(async () => {
          const data = await updateAuditStatus(id, {
            status: status,
          });
          this.$message.success("操作成功");
          this.getAllSupplierWithdrawal();
        });
      },
      deleteSelt(id) {
        this.deleteSel = true;
        this.del_id = id;
      },
      async deleteSon() {
        this.$refs.add_form.validate(async (valid) => {
          if (valid) {
            const data = await updateAuditStatus(this.del_id, {
              status: 3,
              reason: this.add_form.delete_reason,
            });
            this.deleteSel = false;
            this.$message.success("操作成功");
            this.getAllSupplierWithdrawal();
          }
        });
      },
      clearSupplier() {
        this.searchDate.supplierId = "";
        this.pageChange(1);
      },
      selUnitSupplier(val) {
        this.pageChange(1);
      },
    },
  };
</script>

<style scoped></style>
