<script>
import {mapState} from "vuex"
import AiEartagPicker from "@project/xumu/components/AiEartagPicker.vue";
import AiInput from "dui/packages/basic/AiInput.vue";

const columns = [
  {label: "序号", type: "index"},
  {label: "生物芯片耳标号", prop: "biochipEarNumber"},
  {label: "类别", prop: "category", dict: "category"},
  {label: "品种", prop: "variety", dict: "variety"},
]
export default {
  name: "iaAdd",
  components: {AiInput, AiEartagPicker},
  props: {
    instance: Function,
    permissions: Function,
    dict: Object
  },
  data() {
    return {
      detail: {detailList: []},
      columns,
      companyList: []
    }
  },
  computed: {
    ...mapState(["user"]),
    userinfo: v => v.user.info || {},
    pageTitle: v => {
      const appName = v.$parent.menuName || v.$parent.$options.label
      return v.$route.query.id ? v.isEdit ? `编辑${appName}` : `${appName}详情` : `新增${appName}`
    },
    isAdd: v => !v.$route.query.id,
    isEdit: v => v.$route.query.edit == 1,
  },
  methods: {
    back(params = {}) {
      this.$router.push(params)
    },
    getDetail() {
      const {id} = this.$route.query
      return id && this.instance.post("/api/insurance/apply/getInfo", null, {params: {id}}).then(res => {
        if (res?.data) {
          const detail = res.data
          detail.detailList = detail.weightList || []
          return this.detail = {...detail}
        }
      })
    },
    getCompanies() {
      this.instance.post("/api/insurance/apply/getCompany").then(res => {
        if (res?.data) {
          this.companyList = res.data
        }
      })
    },
    getProducts(id) {
      const item = this.companyList.find(e => e.id == id)
      return item?.children || []
    },
    submit() {
      this.$refs.detail.validate().then(() => {
        this.form.biochipEarNumber = this.detail.biochipEarNumber
        this.instance.post("/api/insurance/apply/addOrEdit", this.form).then(res => {
          if (res?.code == 0) {
            this.dialog = false
            this.getDetail()
          }
        })
      })
    }
  },
  created() {
    this.getCompanies()
    this.getDetail()
  }
}
</script>

<template>
  <ai-page :title="pageTitle" class="iaAdd" showBack content-string="blank">
    <el-form size="small" label-width="120px" :model="detail" ref="detail">
      <ai-card title="基础信息">
        <div class="grid">
          <el-form-item label="养殖场" prop="farmId" :rules="{required:true,message:'请选择 养殖场'}">
            <ai-select v-if="isAdd||isEdit" v-model="detail.farmId" :instance="instance" :action="`/api/siteUser/querySiteByUserId?userId=${userinfo.id}`" :prop="{label:'name'}"/>
            <b v-else v-text="detail.farmName"/>
          </el-form-item>
          <el-form-item label="承保公司" prop="companyId" :rules="{required:true,message:'请选择 承保公司'}">
            <ai-select v-if="isAdd||isEdit" v-model="detail.companyId" :select-list="companyList" :prop="{label:'name'}"/>
            <b v-else v-text="detail.companyName"/>
          </el-form-item>
          <el-form-item label="保险产品" prop="productType" :rules="{required:true,message:'请选择 保险产品'}">
            <ai-select v-if="isAdd||isEdit" v-model="detail.productType" :select-list="getProducts(detail.companyId)" :prop="{label:'name',value:'productType'}"
                       @select="v=>$set(detail,'insureType',v.children[0].insureType)"/>
            <b v-else v-text="detail.productType"/>
          </el-form-item>
          <el-form-item label="投保类型">
            <ai-input :value="dict.getLabel('insureType',detail.insureType)" placeholder="根据保险产品自动带出" :edit="isAdd||isEdit" readonly/>
          </el-form-item>
          <el-form-item label="联系人">
            <ai-input v-model="detail.contacts" :edit="isAdd||isEdit"/>
          </el-form-item>
          <el-form-item label="联系电话">
            <ai-input v-model="detail.phone" :edit="isAdd||isEdit"/>
          </el-form-item>
        </div>
      </ai-card>
      <ai-card title="投保对象">
        <template #right>
          <ai-eartag-picker @select="v=>detail.detailList=v" :instance="instance"
                            :action="`/insurance/apply/getEarNumberList?farmId=${detail.farmId}`">
            <el-button type="text">选择</el-button>
          </ai-eartag-picker>
        </template>
        <ai-table :tableData="detail.detailList" :colConfigs="columns" :isShowPagination="!1" hideOptions/>
      </ai-card>
      <ai-card title="证件信息">

      </ai-card>
    </el-form>
    <div slot="footer">
      <el-button @click="back">返回</el-button>
    </div>
  </ai-page>
</template>

<style scoped lang="scss">
.iaAdd {
}
</style>
