<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-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="保险公司">
            <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="申请项目">
            <a-select v-model="form.authProject" placeholder="请选择项目" allowClear>
              <a-select-option v-for="(item) in ProjectList" :key="item.id" :value="item.refcd">{{item.descCh}}</a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
        <a-col :lg="6" :sm="12">
          <a-form-model-item label="申请状态">
            <a-select v-model="form.authorStatus" placeholder="请选择申请状态" allowClear>
              <a-select-option v-for="(item,i) in applyStatusOptions" :key="i" :value="item.value">{{item.name}}</a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
        <a-col :lg="6" :sm="12">
          <a-form-model-item label="病历号">
            <a-input v-model="form.mrnNo" placeholder="请输入病历号" allow-clear />
          </a-form-model-item>
        </a-col>
        <a-col :lg="6" :sm="12">
          <a-form-model-item label="客户姓名">
            <a-input v-model="form.patientName" placeholder="请输入客户姓名" allow-clear />
          </a-form-model-item>
        </a-col>
        <a-col :lg="6" :sm="12">
          <a-form-model-item label="预授权日期">
            <a-range-picker format="YYYY年MM月DD日" v-model="form.dateRange2"  :placeholder="['开始日期', '结束日期']" />
          </a-form-model-item>
        </a-col>
        <a-col :lg="6" :sm="12" class="none-label">
          <a-form-model-item label="button">
            <a-button type="primary" @click="handlerSearch">
              <Icon name="ssisearch_active" :size="12" />查询
            </a-button>
            <a-button class="mar-left5" type="primary" @click="reset">
              <Icon name="ssireset" :size="12" />重置
            </a-button>
            <!-- <a-button class="mar-left5" type="primary" @click="addPreAuth">
              <Icon name="ssiadd" :size="12" />新建预授权</a-button> -->
            </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
    <a-table :columns="columns" :data-source="dataList" :scroll="{ x: true }" :pagination="false">
      <template slot="authorStatus" slot-scope="text">
        {{text | formatApplyStatus}}
      </template>
      <template slot="authProject" slot-scope="text">
        {{formatProject(text)}}
      </template>
      <template slot="rejectReason" slot-scope="text">
        {{formatReason(text)}}
      </template>
      <template slot="authorizeItemVoList" slot-scope="text, record">
        <a-button type="link" @click.stop="seeEvt(record)">查看</a-button>
      </template>
      <template slot="operation" slot-scope="text, record, index">
        <a-button type="link" @click.stop="editEvt(record)">修改</a-button>
        <a-button type="link" class="danger" @click.stop="delRecord(index)">删除</a-button>
      </template>
    </a-table>
    <!--分页-->
    <BurtPagination :pagination="pagination" @pageChange="getData" />
    <!--预授权费用数据-->
    <authorizeVoList ref="authData" />
  </div>
</template>

<script>
import BurtPagination from "@/components/Customers/pagation";
import authorizeVoList from './components/authorizeVoList';
import moment from 'moment'
import {applyStatusOptions} from '@/assets/js/utilsdictOptions';
import mixins from '@/mixins/index';
export default{
  data(){
    const columns = [
      { title: "病历号", dataIndex: "mrnNo", key:"receiptNo",align:'center', width: 136},
      { title: "客户姓名", dataIndex: "patientName", key:"patientNo",align:'center', width: 136},
      { title: "保险公司", dataIndex: "payorName", width: 110 },
      { title: "申请项目", dataIndex: "authProject", width: 100, scopedSlots: { customRender: "authProject" }},
      { title: "申请金额", dataIndex: "authorAmount", width: 100, align: 'center' },
      { title: "申请状态", dataIndex: "authorStatus", width: 100, align: 'center', scopedSlots: { customRender: "authorStatus" } },
      { title: "申请日期", dataIndex: "createDate", width: 130, align: 'center' },
      { title: "批准金额", dataIndex: "approvalAmount", width: 100, align: 'center' },
      { title: "不予批准原因", dataIndex: "rejectReason", width: 120, align: 'center', scopedSlots: { customRender: "rejectReason" } },
      { title: "预授权使用情况", dataIndex: "authorizeItemVoList", width: 130, align: 'center', scopedSlots: { customRender: "authorizeItemVoList" }  },
      // { title: "备注", dataIndex: "remark", width: 100, align: 'center' },
      { title: "操作", dataIndex: "operation", scopedSlots: { customRender: "operation" }, fixed: "right", width: "200px", align: 'center'},
    ];
    return {
      columns,
      applyStatusOptions,
      form: {
        dateRange: [],
        dateRange2: [],
        payorCode: '',
        authProject: '',
        authorStatus: '',
        mrnNo: '',
        patientName: '',
      },
      companyOptions: [],  //保险公司
      ProjectList: [], //预授权项目
      rejectReason: [], //拒绝原因
      dataList: [],
      pagination: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
      },
    }
  },
  mixins: [mixins],
  components: {
    BurtPagination, authorizeVoList,
  },
  created(){
    this.getData();
    this._getCompanyOptions();
    this.getRefcdByRefgrp();
  },
  methods: {
    moment,
    formatProject(val){
			if (!val) {
			  return;
			}
			const item = this.ProjectList.find((item) => {
			  return item.refcd == val;
			});
			return item? item.descCh: "";
		},
    formatReason(val){
			if (!val) {
			  return;
			}
			const item = this.rejectReason.find((item) => {
			  return item.refcd == val;
			});
			return item? item.descCh: "";
		},
    reset(){
      this.form = {
        dateRange: [],
        payorCode: '',
        authProject: '',
        authorStatus: '',
        mrnNo: '',
        patientName: '',
      };
    },
    //新建预授权
    addPreAuth(){
      this.$router.push({
        path: '/pre-auth/add'
      })
    },
    handlerSearch(){
      this.pagination.pageNum = 1;
      this.getData();
    },
    // 获取保险公司下拉选项
    _getCompanyOptions() {
      this.$apis.getCompanyOptions().then((res) => {
        this.companyOptions = res.content || [];
      });
    },
    // 获取码表
    getRefcdByRefgrp() {
      //拒绝原因
      this.$apis.getRefcdByRefgrp({
        modid: "CI",
        refgrp: "REJECT_REASON"
      }).then((res) => {
        this.rejectReason = res.content || [];
      });
      //预授权项目
      this.$apis.getRefcdByRefgrp({
        modid: "CI",
        refgrp: "AUTH_PROJECT"
      }).then((res) => {
        this.ProjectList = res.content || [];
      });
    },
    // 获取列表数据
    getData() {
      let filter = {
        pageNum: this.pagination.pageNum,
        pageSize: this.pagination.pageSize,
        ...this.form,
        createDateStart: this.form.dateRange[0]?moment(this.form.dateRange[0]).format('YYYY-MM-DD 00:00:00'):'',
        createDateEnd: this.form.dateRange[1]?moment(this.form.dateRange[1]).format('YYYY-MM-DD 00:00:00'):'',
        startDate: this.form.dateRange2[1]?moment(this.form.dateRange2[1]).format('YYYY-MM-DD 00:00:00'):'',
        endDate: this.form.dateRange2[1]?moment(this.form.dateRange2[1]).format('YYYY-MM-DD 00:00:00'):'',
      }
      delete filter.dateRange;
      delete filter.dateRange2;
      this.$apis.getAuthorizeList(filter)
      .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);
        }
      });
    },
    editEvt(record){
      this.$router.push({
        path: '/pre-auth/add',
        query: {
          id: record.id
        }
      })
    },
    //查看预授权使用数据
    seeEvt(record={}){
      this.$refs.authData.showModal1(record);
    },
    addEvt(){

    },
    //删除记录
    delRecord(index) {
      this.$modal.confirm({
        title: "删除",
        content: "确定删除该条记录？",
        okText: "确认",
        cancelText: "取消",
        onOk: () => {
          this.$apis.authorizeDelete({
            id: this.dataList[index].id,
          })
          .then((res) => {
            if (res.returnCode == "0000") {
              this.$message.success("删除成功");
              this.dataList.splice(index, 1);
            } else {
              this.$message.error(res.returnMsg);
            }
          });
        },
        onCancel: () => {},
      });
    }
  }
}
</script>

<style lang="less" scoped>
.mar-left5{
  margin-left: 5px;
}
</style>