<template>
  <div class="white_bg burt-container">
    <!-- form -->
    <a-form-model ref="form" layout="vertical" :model="form">
      <a-row :gutter="30">
        <a-col :lg="6" :sm="12">
          <a-form-model-item label="保险公司">
            <a-select v-model="form.payorCode" placeholder="请选择保险公司" allowClear>
              <a-select-option v-for="item in companyOptions" :key="item.id" :value="item.payorCode">
                {{ item.longName }}
              </a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
        <a-col :lg="6" :sm="12">
          <a-form-model-item label="EOB收到日期">
            <a-range-picker format="YYYY年MM月DD日" v-model="form.dateRange" :placeholder="['开始日期', '结束日期']" />
          </a-form-model-item>
        </a-col>
        <a-col :lg="6" :sm="12">
          <a-form-model-item label="EOB名称">
            <a-input v-model="form.eobName" placeholder="请输入EOB名称" />
          </a-form-model-item>
        </a-col>
        <a-col :lg="6" :sm="12">
          <a-form-model-item label="EOB金额(人民币)">
            <a-input type="number" v-model="form.eobAmountCny" placeholder="请输入金额"/>
          </a-form-model-item>
        </a-col>
        <a-col :lg="6" :sm="12">
          <a-form-model-item label="EOB金额(美元)">
            <a-input type="number" v-model="form.eobAmountUsd" placeholder="请输入金额"/>
          </a-form-model-item>
        </a-col>
        <a-col :lg="6" :sm="12">
          <a-form-model-item label="EOB状态">
            <a-select v-model="form.status" placeholder="请选择">
              <a-select-option value="1">已寄送</a-select-option>
              <a-select-option value="2">未寄送</a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
        <a-col :md="24" :lg="12" class="none-label">
          <a-form-model-item label="button">
            <a-button type="primary">导出</a-button>
            <a-button class="mar-left10" type="primary" @click="addNewEvt">
              <Icon name="ssiadd" :size="14" />新建EOB
            </a-button>
            <a-button class="mar-left10" type="primary" @click.stop="handlerReset">
              <Icon name="ssireset" :size="14" />重置
            </a-button>
            <a-button class="mar-left10" type="primary" @click="handlerSearch">
              <Icon name="ssisearch_active" :size="14" />查询
            </a-button>
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
    <!-- table -->
    <a-table :columns="columns" :data-source="dataList" :scroll="{ x: true }" :pagination="false"
      :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }">
      <template slot="visitTimeStart" slot-scope="text, record">
        <span>{{record.visitTimeStart? moment(record.visitTimeStart).format('YYYY-MM-DD'):''}}</span> ~ 
        <span>{{record.visitTimeEnd? moment(record.visitTimeEnd).format('YYYY-MM-DD'):''}}</span>
      </template>
      <template slot="sendSts" slot-scope="record, text">
        <span>{{text==1?'已寄送':'未寄送'}}</span>
      </template>
      <template slot="operation" slot-scope="text, record, index">
        <a-button type="link">修改</a-button>
        <a-button v-if="record.eobNo" type="link" class="danger" @click.stop="delRecord(index)">删除</a-button>
      </template>
    </a-table>
    <BurtPagination :pagination="pagination" @pageChange="getData" />
  </div>
</template>

<script>
import BurtPagination from "@/components/CUSTOMER/pagation";
import moment from "moment";
const columns = [
  { title: "病历号", dataIndex: "patientNo", ellipsis: true, width: 100 },
  { title: "客户姓名", dataIndex: "patientName", ellipsis: true, width: 85 },
  { title: "保险公司", dataIndex: "payorName", ellipsis: true, width: 80 },
  { title: "就诊日期", dataIndex: "visitTimeStart", ellipsis: true, width: 110, scopedSlots: { customRender: "visitTimeStart" } },
  { title: "理赔状态", dataIndex: "sendSts", ellipsis: true, width: 90, scopedSlots: { customRender: "sendSts" } },
  { title: "账单金额", dataIndex: "receiptAmount", ellipsis: true, width: 85 },
  { title: "自付金额", dataIndex: "selfpaidAmount", ellipsis: true, width: 85 },
  { title: "理赔金额", dataIndex: "eobPaidAmount", ellipsis: true, width: 85 },
  { title: "回款金额", dataIndex: "patientName7", ellipsis: true, width: 85 },
  { title: "未清金额", dataIndex: "eobRefuseAmount", ellipsis: true, width: 85 },
  { title: "保险公司欠费", dataIndex: "patientName11", ellipsis: true, width: 110, },
  { title: "个人欠费", dataIndex: "patientName9", ellipsis: true, width: 85 },
  { title: "备注", dataIndex: "sendRemark", ellipsis: true, width: 120 },
  { title: "操作", dataIndex: "operation", scopedSlots: { customRender: "operation" }, fixed: "right", width: "170px"},
];
export default {
  data() {
    return {
      columns,
      form: {
        dateRange: [],
        payorCode: '',
        eobName: '',
        eobAmountCny: '',
        eobAmountUsd: '',
        status: ''
      },
      dataList: [],
      companyOptions: [], //保险公司
      pagination: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
      },
      selectedRowKeys: [], // Check here to configure the default column
    };
  },
  components: {
    BurtPagination,
  },
  created(){
    this.getData();
    this._getCompanyOptions();
  },
  methods: {
    moment,
    onSelectChange(selectedRowKeys) {
      let arr = [];
      for(let i=0; i<selectedRowKeys.length; i++){
        let idx = selectedRowKeys[i];
        if(this.dataList[idx].eobNo){
          this.$msg.destroy();
          this.$message.warning('EOB编号已存在')
        }else{
          arr.push(idx);
        }
      }
      this.selectedRowKeys = arr;
    },
    handlerSearch() {
      this.pagination.pageNum = 1;
      this.getData();
    },
    // 重置
    handlerReset() {
      this.form = {
        dateRange: [],
        payorCode: '',
        eobName: '',
        eobAmountCny: '',
        eobAmountUsd: '',
        status: ''
      }
    },
    // 获取保险公司下拉选项
    _getCompanyOptions() {
      this.$apis.GETCOMPANYOPTIONS().then((res) => {
        this.companyOptions = res.content || [];
      });
    },
    getData() {
       this.$apis.QUERYCIRECEIPSENDLIST({
        pageNum: this.pagination.pageNum,
        pageSize: this.pagination.pageSize,
       })
      .then((res) => {
        if (res.returnCode == "0000") {
          let content = res.content || {};
          this.pagination.total = content.total || 0;
          this.dataList = content.list || [];
        } else {
          this.$message.error(res.returnMsg);
        }
      });
    },
    //删除记录
    delRecord(index) {
      this.$modal.confirm({
        title: "删除",
        content: "确定删除该条记录？",
        okText: "确认",
        cancelText: "取消",
        onOk: () => {
          this.$apis.DELEOBRECEIPTINFO({
            eobNo: this.dataList[index].eobNo,
          })
          .then((res) => {
            if (res.returnCode == "0000") {
              this.$message.success("删除成功");
              this.dataList.splice(index, 1);
            } else {
              this.$message.error(res.returnMsg);
            }
          });
        },
        onCancel: () => {},
      });
    },
    //新建EOB
    addNewEvt(){
      if(this.selectedRowKeys.length == 0){
        this.$message.warning("未选择EOB");
        return;
      }
      let eobReceiptList = [];
      this.dataList.forEach((item,index)=>{
        if(this.selectedRowKeys.indexOf(index)!=-1){
          eobReceiptList.push({
            id: item.id,
            receiptNo: item.receiptNo,
          })
        }
      })
      let formData = {
        ...this.form,
        eobReceiptList: eobReceiptList,
        eobDate: moment(this.form.dateRange[0]).format('YYYY-MM-DD 00:00:00')
      }
      delete formData.dateRange;
      this.$apis.SAVEEOBRECEIPTINFO(formData)
      .then((res) => {
        if (res.returnCode == "0000") {
          this.$message.success("新建成功");
          this.handlerReset();
          this.selectedRowKeys = [];
          this.getData();
        } else {
          this.$message.error(res.returnMsg);
        }
      });
    }
  },
};
</script>

<style lang="less" scoped>
.none-label {
  text-align: right;
  .ant-form-item-label {
    opacity: 0;
  }
}
.ant-btn .icon-class {
  .mg-r(10);
}
.success.ant-btn-link {
  color: #4cd964;
}
.danger.ant-btn-link {
  color: #ff3b30;
}
</style>
