<script>
import {mapState} from "vuex"

const columns = [
  {label: "序号", type: "index"},
  {label: "投保单号", prop: "orderNo"},
  {label: "所属养殖户", prop: "applyName"},
  {label: "所属养殖场", prop: "farmName"},
  {label: "投保数量(头)", prop: "insureNumber", width: 120},
  {label: "投保状态", prop: "status", width: 160, dict: "insureStatus"},
  {label: "投保类型", prop: "insureType", dict: "insureType"},
  {label: "投保时间", prop: "createTime"},
  {label: "可理赔数量", prop: "unpaidClaimNumber", width: 120},
  {label: "说明", prop: "remarks"},
]
export default {
  name: "claimList",
  props: {
    instance: Function,
    dict: Object,
    permissions: Function
  },
  data() {
    return {
      columns,
      tableData: [],
      page: {pageNum: 1, pageSize: 10, total: 0},
      search: {},
      dialog: false,
      form: {}
    }
  },
  computed: {
    ...mapState(['user']),
    userinfo: v => v.user.info || {},
    pageTitle: v => v.$parent.menuName || v.$parent.$options.label
  },
  watch: {
    search: {
      deep: true,
      handler() {
        this.page.pageNum = 1
        this.getTableData()
      }
    }
  },
  methods: {
    getTableData() {
      this.instance.post("/api/insurance/claim/apply/page", {...this.page, ...this.search}).then(res => {
        if (res?.data) {
          this.tableData = res.data?.records.map(e => ({...e, permit: e.remarks == "可申请理赔"}))
          this.page.total = res.data.total
        }
      })
    },
  },
  created() {
    this.getTableData()
  }
}
</script>

<template>
  <ai-page class="claimList" :title="pageTitle">
    <ai-search-bar>
      <template #left>
        <ai-input placeholder="投保订单号" v-model="search.orderNo"/>
        <ai-select placeholder="全部投保类型" v-model="search.insureType" dict="insureType"/>
        <ai-search label="投保日期">
          <el-date-picker value-format="yyyy-MM-dd HH:mm:ss" v-model="search.beginDate" type="datetime" placeholder="开始日期" size="small"/>
          <el-date-picker value-format="yyyy-MM-dd HH:mm:ss" v-model="search.endDate" type="datetime" placeholder="结束日期" size="small"/>
        </ai-search>
        <ai-input placeholder="养殖户" v-model="search.applyName"/>
        <ai-input placeholder="养殖场" v-model="search.farmName"/>
      </template>
    </ai-search-bar>
    <ai-search-bar>
      <template #left>
        <ai-download :instance="instance" url="/api/insurance/claim/apply/export" :params="{...search,...page}" :fileName="`${pageTitle}导出表-${Date.now()}`"/>
      </template>
    </ai-search-bar>
    <ai-table :tableData="tableData" :colConfigs="columns" :dict="dict" @getList="getTableData"
              :total="page.total" :current.sync="page.pageNum" :size.sync="page.pageSize">
      <el-table-column slot="options" label="操作" fixed="right" align="center">
        <template slot-scope="{row}">
          <div class="table-options">
            <template v-if="row.permit">
              <el-button type="text" @click="dialog=true,$set(form,'id',row.orderNo)">理赔</el-button>
            </template>
            <el-button v-else type="text" @click="$router.push({hash:'#add',query:{id:row.orderNo}})">查看</el-button>
          </div>
        </template>
      </el-table-column>
    </ai-table>
    <ai-dialog v-model="dialog" title="理赔须知" @closed="form={}">
      <el-form size="small" label-position="top" :mode="form" ref="form">
        <el-form-item label="如遇一下情况进行赔付：">
          1、自然灾害：如暴雨、洪水、台风、冰雹、雷击、暴风雪等导致的肉牛死亡或伤残；<br/>
          2、疾病与疫病:包括但不限于口蹄疫、布鲁氏菌病、炭疽、牛结核病等对肉牛生命安全造成威胁的疾病；<br/>
          3、意外事故:如火灾、爆炸、触电、盗窃、走失等；<br/>
          4、强制扑杀:由于政府政策或疫情控制需要，对肉牛进行的强制扑杀。
        </el-form-item>
        <el-form-item class="flex center">
          <el-checkbox v-model="form.agree">本人阅读并知晓理赔须知，承认上传资料的真实性</el-checkbox>
        </el-form-item>
      </el-form>
      <template #foot>
        <el-button @click="$router.push({hash:'#claim',query:{id:form.id}})" type="primary" :disabled="!form.agree">符合要求，立即申请</el-button>
        <el-button @click="dialog=false">取消</el-button>
      </template>
    </ai-dialog>
  </ai-page>
</template>

<style scoped lang="scss">
.claimList {
  height: 100%;

  .deleteBtn {
    color: $errorColor;
  }
}
</style>
