<template>
  <div class="app-container">
    <a-form :model="queryForm" ref="queryFormRef" layout="inline">

      <a-form-item field="user_name" label="医生姓名">
        <a-input :style="{ width: '182px' }" v-model="queryForm.user_name" placeholder="请输入医生姓名" @press-enter="handleQuery" />
      </a-form-item>
      <a-form-item field="mobile" label="电话号码">
        <a-input :style="{ width: '182px' }" v-model="queryForm.mobile" placeholder="请输入电话号码" @press-enter="handleQuery" />
      </a-form-item>
      <a-form-item field="settle_status" label="结算状态">
        <a-select  @change="changeSettle" v-model="queryForm.settle_status" placeholder="请选择结算状态" :style="{ width: '182px' }">
          <a-option :value="1">待结算</a-option>
          <a-option :value="2">结算中</a-option>
          <a-option :value="3">结算成功</a-option>
          <a-option :value="4">结算失败</a-option>
        </a-select>
      </a-form-item> 
      <!-- <a-form-item field="examine_status" label="审核状态">
        <a-select  v-model="queryForm.examine_status" placeholder="请选择审核状态" :style="{ width: '182px' }">
          <a-option :value="1">审核中</a-option>
          <a-option :value="2">审核通过</a-option>
          <a-option :value="3">审核未通过</a-option>
        </a-select>
      </a-form-item> 
      <a-form-item field="payment_status" label="打款状态">
        <a-select  v-model="queryForm.payment_status" placeholder="请选择打款状态" :style="{ width: '182px' }">
          <a-option :value="0">否</a-option>
          <a-option :value="1">是</a-option>
        </a-select> 
      </a-form-item> -->
      <a-form-item field="examine_range_time" label="审核时间范围">
        <a-range-picker
          style="width: 260px"
          v-model="queryForm.examine_range_time"
        />
      </a-form-item>
      <a-form-item field="payment_range_time" label="打款时间范围">
        <a-range-picker
          style="width: 260px"
          v-model="queryForm.payment_range_time"
        />
      </a-form-item>
      <a-form-item>
        <a-space>
          <a-button type="primary" @click="handleQuery"><icon-search /> 搜索</a-button>
          <a-button @click="handleResetQuery"><icon-loop /> 重置</a-button>
        </a-space>
      </a-form-item>
    </a-form>

    <a-divider />

    <!-- action -->
    <div class="action">
      <a-space>
        <a-button v-has="'admin:sysFinancialRecord:selectExport'" type="primary" @click="handlExport(2)"><icon-export /> 选择数据导出 </a-button>
        <a-button v-has="'admin:sysFinancialRecord:searchExport'" type="primary"  @click="handlExport(1)"><icon-export /> 当前搜索全部导出</a-button> 
        <a-button v-has="'admin:sysFinancialRecord:allExport'" type="primary"  @click="handlExport(3)"><icon-export /> 全部导出</a-button> 
      </a-space>
    </div>

    <!-- table -->
    <a-table :columns="columns" :data="tableData" 
    :scroll="{ x:2000 }"
     ref="tableRef"
      :row-selection="{ type: 'checkbox', showCheckedAll: true }"
      :pagination="{ 'show-total': true, 'show-jumper': true, 'show-page-size': true, total: pager.total, current: currentPage }"
      row-key="withdrawal_id" @selection-change="(selection) => {deleteData = selection;}"
      @page-change="handlePageChange" @page-size-change="handlepage_sizeChange" >
      <template #doctor_id="{record,rowIndex}">
        <div>{{(rowIndex+1)+(pager.page-1)*pager.page_size}}</div>
      </template>
      <template #payment_status="{ record }">
        <!-- 状态（0:禁用 1:正常 2:删除） -->
        <div v-if="record.examine_status==1 && record.payment_status==0">待结算</div>
        <div v-else-if="record.examine_status==2 && record.payment_status==0">结算中</div>
        <div v-else-if="record.examine_status==2 && record.payment_status==1">结算成功</div>
        <div v-else-if="record.examine_status==3 ">结算失败</div>
<!--           
        <a-tag v-if="record.payment_status == 1" color="green">正常</a-tag>
        <a-tag v-else color="red">失败</a-tag> -->
      </template>
      

      <template #created_at="{record}">
        <div>{{parseTime(record.created_at)}}</div>
      </template>
      <template #payment_time="{record}">
        <div>{{parseTime(record.payment_time)}}</div>
      </template>
      <template #action="{ record }">
        <a-space>
          <a-button v-has="'admin:sysFinancialRecord:detail'" type="text"
            @click="handleDetail(record)"><icon-book />详情</a-button>
          <!-- <a-button v-has="'admin:sysFamilyList:edit'" type="text" @click="handleUpdate(record)"><icon-edit />
            修改</a-button> -->
          <!-- <a-button v-has="'admin:sysFamilyList:remove'" type="text"
            @click="() => { deleteVisible = true; deleteData = [record.doctor_id];  }"><icon-delete /> 删除</a-button> -->
        </a-space>
      </template>
    </a-table>

    <withdrawalModal :modalVisible="modalVisible" :modalForm="modalForm" @familyVisibleChange="()=>{modalVisible=false,modalForm.withdrawal_id=''}" @freshDetail="freshDetail"></withdrawalModal>
    <!-- Akiraka 20230223 删除与批量删除 开始 -->
    <!-- <DeleteModal :data="deleteData" :visible="deleteVisible" :apiDelete="removeFamily"
      @deleteVisibleChange="() => deleteVisible = false" /> -->
    <!-- Akiraka 20230223 删除与批量删除 结束 -->
    <!-- <a-modal v-model:visible="okVisible" :modal-style="{width:'320px'}" body-class="okmodal" 
      @cancel="()=>okVisible=false">
      <template #title>
        提示
      </template>
      <div>确定保存当前信息？</div>
    </a-modal> -->
  </div>
</template>

<script setup>
  import { reactive, ref, getCurrentInstance, onMounted, nextTick, watch, computed, withDirectives } from 'vue';
  import { getWithdrawalList,getWithdrawalDetail,exportRecord} from '@/api/finance/record';
  import { parseTime } from '@/utils/parseTime';
  import {downloadFile} from "@/utils/downloadFile"
  // Akiraka 20230210 删除数据
  const deleteData = ref([])
  // Akiraka 20230210 删除对话框
  const deleteVisible = ref(false)
  // Akiraka 20230210 监听删除事件
  const okVisible = ref(false);


  watch(() => deleteVisible.value, (value) => {
    if (value == false) {
      getWithdrawalInfo(pager);
    }
  });

  const file = ref();
  const { proxy } = getCurrentInstance();
  const currentPage = ref(1);
  // Pager
  const pager = {
    total: 0,
    page: 1,
    page_size: 10,
  };
  // form
  const queryForm = reactive({

  });
  const modalForm = reactive({
    user:{},
    avatar: 'https://img.applets.igandanyiyuan.com/basic/file/patient_avatar.png',
 
  });
  watch(() => queryForm.examine_range_time,
  (value) => {
    if (value) {
      let [startTime, endTime] = value;
      queryForm.examine_time = startTime + '&' + endTime;
    } else {
      queryForm.examine_time = '';
    }
  }
);
watch(() => queryForm.payment_range_time,
  (value) => {
    if (value) {
      let [startTime, endTime] = value;
      queryForm.payment_time = startTime + '&' + endTime;
    } else {
      queryForm.payment_time = '';
    }
  }
);
  // Modal
  const modalVisible = ref(false);
  const modalTitle = ref('默认标题');
 
  // Batch Del List
  let batchList = [];

  // Table Columns
  const columns = [
    { title: '编号', dataIndex: 'doctor_id', slotName: 'doctor_id', width: '90' },
    { title: '医生姓名', dataIndex: 'doctor_name',width:200 },
    { title: '应提现金额(元)', dataIndex: 'applied_withdrawal_amount',slotName: 'applied_withdrawal_amount', width:180 },
    { title: '实际提现金额(元)', dataIndex: 'actual_withdrawal_amount',slotName: 'actual_withdrawal_amount',width:180 },
    { title: '个人所得税(元)', dataIndex: 'income_tax', slotName: 'income_tax',width: 150 },
    { title: '开户行', dataIndex: 'bank_name', slotName: 'bank_name',width: 150 },
    { title: '开户城市', dataIndex: 'bank_city', slotName: 'bank_city',width: 150 },
    { title: '状态', dataIndex: 'payment_status', slotName: 'payment_status' },
    { title: '提现时间', dataIndex: 'created_at', slotName: 'created_at' },
    { title: '打款时间', dataIndex: 'payment_time', slotName: 'payment_time' },
    { title: '操作人', dataIndex: 'examine_by', slotName: 'examine_by',width: 80 },
    { title: '操作', slotName: 'action', fixed: "right", width: 180 },
  ];

  // Table Data
  const tableData = ref([]);
  //弹框状态
  const modalSatus = ref('add');

  // 新增Satus
  const handleAdd = () => {
    modalVisible.value = true;
    modalTitle.value = '新增患者';
    modalSatus.value = 'add';
    modalForm.patient_id = null;
  };
  
  //详情
  const handleDetail = async (record) => {
    const { code, data, message } = await getWithdrawalDetail(record.withdrawal_id);

    if (code == 200) {
        Object.assign(modalForm, data);
        modalVisible.value = true;
      }
    };
    const freshDetail=()=>{
      handleDetail({
        withdrawal_id:modalForm.withdrawal_id
      })
  }
  // 批量删除
  const handleBatchDelete = () => {
    if (batchList.length !== 0) {
      proxy.$modal.warning({
        title: '提示',
        content: '是否批量删除以下选中的数据？',
        hideCancel: false,
        onOk: async () => {
          const res = await removeFamily({ ids: batchList });
          proxy.$message.success(res.message);
          getWithdrawalInfo(pager);
        },
        onCancel: () => {
          proxy.$message.info('已取消批量删除数据');
        },
      });
    } else {
      proxy.$message.error('请勾选需要删除的数据！');
    }
  };
  const changeSettle=(value)=>{
    // <div v-if="record.examine_status!=3 && record.payment_status==0">待结算</div>
    //     <div v-else-if="record.examine_status==2 && record.payment_status==0">结算中</div>
    //     <div v-else-if="record.examine_status==2 && record.payment_status==1">结算成功</div>
    //     <div v-else-if="record.examine_status==3 ">结算失败</div>
    if(value==1){
      queryForm.examine_status=1;
      queryForm.payment_status=0;
    }else if(value==2){
      queryForm.examine_status=2;
      queryForm.payment_status=0;
    }else if(value==3){
      queryForm.examine_status=2;
      queryForm.payment_status=1;
    }else if(value==4){
      queryForm.examine_status=3;
      delete queryForm.payment_status;
    }else{
      delete queryForm.examine_status;
     delete queryForm.payment_status;
    }
  }
  /**
   * 分页改变
   * @param {Number} [page]
   */
  const handlePageChange = (page) => {
    pager.page = page;

    // 修改当前页码
    currentPage.value = page;
    getWithdrawalInfo({ ...pager, ...queryForm });
  };

  // 每页数据量
  const handlepage_sizeChange = (page_size) => {
    pager.page_size = page_size;
    getWithdrawalInfo({ ...pager, ...queryForm });
  };

  // 获取患者信息
  const getWithdrawalInfo = async (params = {}) => {
    const { data, code, message } = await getWithdrawalList(params);
    if (code == 200) {
      tableData.value = data.data;
      Object.assign(pager, { total: data.total, page: data.page, page_size: data.page_size });
    }
  };

  // 查询患者信息
  const handleQuery = async () => {
    pager.page = 1;
    const params = {
      page: pager.page,
      page_size: pager.page_size,
      ...queryForm,
    };
    getWithdrawalInfo(params);
  };

  // 重置搜索
  const handleResetQuery = () => {
    delete queryForm.examine_status;
    delete queryForm.payment_status;
    proxy.$refs.queryFormRef.resetFields();
    handleQuery();
  }
  const tableRef = ref()
// const selectAll=(check)=>{
//   console.log(check);
// }
const handlExport=async(type)=>{
  proxy.$loading.show();
  let fromData=null;
  if(type==1){
    fromData={
      type,
      ...queryForm
    }
  }else if(type==2){
      if(deleteData.value.length==0){
        proxy.$message.warning('请勾选数据');
        proxy.$loading.hide();
        return false;
      };
    let id='';
    deleteData.value.forEach((item)=>{
      if(id){
        id+=","+item
      }else{
        id=item;
      }
    })
    fromData={
      type,
     id
    }
  }else if(type==3){
    tableRef.value.selectAll(false)
    fromData={
      type
    }
  }
  const {code,data}=await exportRecord(fromData);
 if(code==200){
  downloadFile(data,'提现记录');
 }
  proxy.$loading.hide();
}
  onMounted(() => {
    getWithdrawalInfo(pager);
  });
</script>

<style lang="scss" scoped>
  .action {
    margin-bottom: 12px;
  }

  .okmodal div {
    text-align: center;
  }

  .hospital_name {
    width: 140px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .headImg {
    margin-right: 20px;
    border-radius: 50%;
    width: 80px;
    height: 80px;
  }

  .arco-form-item-layout-horizontal:first-child,
  .arco-form-item-layout-horizontal:nth-child(2) {
    align-items: center;
  }
  .cellbox{
    margin-top: 35px;
  }
  .cellbox .cell{
    width:50%;
    border-bottom:1px dashed #efefef;
    margin-bottom: 20px;
  }
  .cellbox .cell:first-child{
    border: none;
  }
  .cell{
    .arco-form-item{
      margin-bottom: 10px;
    }
  }
  .box {
    display: flex;
    align-items: center;
  }

  .cert .arco-form-item-label-col {
    flex: 0 0 8px !important;
  }

  .red {
    display: inline-block;
    margin-right: 5px;
    font-size: 14px;
    color: red;
    margin-top: 5px;
  }

  .cardNum {
    width: 148px;
  }
  .codbox{
    display: flex;
    align-items: center;
  }
  .reason{
    width:250px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
</style>