<template>
  <div class="info-div">
    <Goback title="保险公司详情" />
    <a-form-model ref="form" layout="vertical" :model="detailObj" :rules="formRules">
      <a-row :gutter="30">
        <a-col :lg="6" :sm="12">
          <a-form-model-item label="保险公司名称" prop="longName">
            <a-input v-model="detailObj.longName" placeholder="保险公司名称" />
          </a-form-model-item>
        </a-col>
        <a-col :lg="6" :sm="12">
          <a-form-model-item label="保险公司英文名称">
            <a-input v-model="detailObj.englishName" placeholder="保险公司英文名称" />
          </a-form-model-item>
        </a-col>
        <a-col :lg="6" :sm="12">
          <a-form-model-item label="国家">
            <a-select v-model="detailObj.country" placeholder="请选择" showSearch>
              <a-select-option v-for="(item) in countyList" :value="item.descCh" :key="item.id">{{item.descCh}}</a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row :gutter="30">
        <a-col :lg="3" :sm="12">
          <a-form-model-item label="有效" prop="status">
            <a-select v-model="detailObj.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 :lg="9" :sm="12">
          <a-row :gutter="30">
            <a-col :lg="12" :sm="12">
              <a-form-model-item label="合同起始日期">
                <a-date-picker v-model="detailObj.startDate" format="YYYY年MM月DD日" placeholder="选择日期"/>
              </a-form-model-item>
            </a-col>
            <a-col :lg="12" :sm="12">
              <a-form-model-item label="合同终止日期">
                <a-date-picker v-model="detailObj.endDate" format="YYYY年MM月DD日" placeholder="选择日期"/>
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-col>
        <a-col :lg="10" :sm="12">
          <a-form-model-item label="保险公司地址" prop="address">
            <a-input v-model="detailObj.address" placeholder="保险公司地址" />
          </a-form-model-item>
        </a-col>
        <a-col :lg="6" :sm="12">
          <a-form-model-item label="网络联系人">
            <a-input v-model="detailObj.contactPerson" placeholder="请输入联系人"/>
          </a-form-model-item>
        </a-col>
        <a-col :lg="8" :sm="12">
          <a-form-model-item label="理赔件邮寄地址">
            <a-input v-model="detailObj.claimAddress" placeholder="理赔件邮寄地址"/>
          </a-form-model-item>
        </a-col>
        <a-col :lg="4" :sm="12">
          <a-form-model-item label="保险公司联系电话" prop="contactPhone">
            <a-input v-model="detailObj.contactPhone" placeholder="保险公司联系电话"/>
          </a-form-model-item>
        </a-col>
        <a-col :lg="6" :sm="12">
          <a-form-model-item label="客服电话" prop="customerTel">
            <a-input v-model="detailObj.customerTel" placeholder="客服电话" />
          </a-form-model-item>
        </a-col>
        <a-col :lg="6" :sm="12">
          <a-form-model-item label="预授权电话">
            <a-input v-model="detailObj.authorTel" placeholder="预授权电话" />
          </a-form-model-item>
        </a-col>
        <a-col :lg="6" :sm="12">
          <a-form-model-item label="保险公司邮件地址" prop="email">
            <a-input v-model="detailObj.email" placeholder="保险公司邮件地址" />
          </a-form-model-item>
        </a-col>
        <a-col :lg="6" :sm="12">
          <a-form-model-item label="客服邮件地址">
            <a-input v-model="detailObj.customerEmail" placeholder="客服邮件地址" />
          </a-form-model-item>
        </a-col>
        <a-col :lg="6" :sm="12">
          <a-form-model-item label="预授权邮件地址">
            <a-input v-model="detailObj.authorEmail" placeholder="预授权邮件地址" />
          </a-form-model-item>
        </a-col>
        <a-col :lg="6" :sm="12">
          <a-form-model-item label="理赔邮件地址">
            <a-input v-model="detailObj.claimEmail" placeholder="理赔邮件地址" />
          </a-form-model-item>
        </a-col>
        <a-col :lg="8" :sm="12">
          <a-form-model-item label="保险公司Protal链接">
            <a-input v-model="detailObj.portalUrl" placeholder="保险公司Protal链接" />
          </a-form-model-item>
        </a-col>
        <a-col :lg="4" :sm="12">
          <a-form-model-item label="登录名">
            <a-input v-model="detailObj.loginName" placeholder="登录名" />
          </a-form-model-item>
        </a-col>
        <a-col :lg="6" :sm="12">
          <a-form-model-item label="密码">
            <a-input type="password" v-model="detailObj.loginPwd" placeholder="密码" />
          </a-form-model-item>
        </a-col>
        <a-col :lg="6" :sm="12">
          <a-form-model-item label="理赔申请书模板">
            <a-upload name="file" :multiple="false" accept="application/pdf" :showUploadList="true"
              :fileList="detailObj.claimApplicationTemplate"
              :customRequest="file => uploadPdf(file, 1)"
              :remove="(file)=> removeFile(file, 1)">
              <a-button type="primary"> <Icon name="ssiupload" :size="18" />上传模板 </a-button>
            </a-upload>
          </a-form-model-item>
        </a-col>
        <a-col :lg="6" :sm="12">
          <a-form-model-item label="预授权申请书模板">
            <a-upload name="file" :multiple="false" accept="application/pdf" :showUploadList="true"
              :fileList="detailObj.authApplicationTemplate"
              :customRequest="file => uploadPdf(file, 2)"
              :remove="(file)=> removeFile(file, 2)">
              <a-button type="primary"> <Icon name="ssiupload" :size="18" />上传模板 </a-button>
            </a-upload>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :sm="24" class="none-label">
          <a-form-model-item label="button">
            <a-button type="primary" @click="handlerSava">
              <Icon :name="detailObj.id?'ssibaocun':'ssiadd'" :size="14" />{{detailObj.id?'保存':'新建'}}
            </a-button>
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
  </div>
</template>

<script>
import Goback from "@/components/CUSTOMER/goback";
export default {
  props: {
    detailObj: {
      claimApplicationTemplate: [],
      authApplicationTemplate: []
    }
  },
  data() {
    return {
      countyList: [],
      formRules: {
        longName: [{ required: true, message: "请输入保险公司名称", trigger: "blur" }],
        status: [{ required: true, message: "请选择是否有效", trigger: "change" }],
        address: [{ required: true, message: "请输入保险公司地址", trigger: "blur" }],
        contactPhone: [{ required: true, message: "请输入保险公司联系电话", trigger: "blur" }],
        customerTel: [{ required: true, message: "请输入客服电话", trigger: "blur" }],
        email: [{ required: true, message: "请输入保险公司邮件地址", trigger: "blur" }],
      },
    }
  },
  components: {
    Goback,
  },
  created(){
    this.getRefcdByRefgrp();
  },
  methods: {
    comfileList1(){
      if(this.detailObj.claimApplicationTemplate){
        return [
          {
            uid: Math.random()*10000,
            name: this.detailObj.claimApplicationTemplate.slice(this.detailObj.claimApplicationTemplate.lastIndexOf('/')+1),
            status: 'done',
            url: this.detailObj.claimApplicationTemplate,
          }
        ]
      }else{
        return [];
      }
    },
    // 获取国家列表
    getRefcdByRefgrp() {
      this.$apis.GETREFCDBYREFGRP({
        modid: "CI",
        refgrp: "COUNTRY_MAPPING"
      }).then((res) => {
        this.countyList = res.content || [];
      });
    },
    //新建保存
    handlerSava(){
      this.$refs.form.validate((valid) => {
        if (valid) {
          let formData = {
            ...this.detailObj,
            claimApplicationTemplate: this.detailObj.claimApplicationTemplate.length>0? this.detailObj.claimApplicationTemplate[0].url: '',
            authApplicationTemplate: this.detailObj.authApplicationTemplate.length>0? this.detailObj.authApplicationTemplate[0].url: '',
          }
          let api = this.detailObj.id? 'PAYORUPDATE': 'CREATEPAY';
          this.$apis[api](formData)
          .then((res) => {
            if (res.returnCode == "0000") {
              this.$message.success("保存成功");
              if(!this.detailObj.id){
                this.$router.replace({
                  path: '/info/companyDetail',
                  query: {
                    id: res.content
                  }
                })
              }
            } else {
              this.$message.error(res.returnMsg);
            }
          });
        }
      });
    },
    removeFile(file,type=1){
      return new Promise((resolve)=>{
        if(type==1){
          this.detailObj.claimApplicationTemplate = [];
        }else{
          this.detailObj.authApplicationTemplate = [];
        }
        resolve();
      });
      
    },
    //上传pdf
    uploadPdf(fileData, type=1){
      let formData = new FormData();
      formData.append("file", fileData.file);
      this.$apis.UPLOADIMG(formData)
      .then((res)=>{
        fileData.onSuccess();
        let fileList = [
          {
            uid: Math.random()*10000,
            name: res.title,
            status: 'done',
            url: res.url,
          }
        ]
        if(type==1){
          this.detailObj.claimApplicationTemplate = fileList;
        }else{
          this.detailObj.authApplicationTemplate = fileList;
        }
        this.$forceUpdate();
      })
    }
  },
};
</script>

<style lang="less" scoped>
.title-div {
  line-height: 56px;
  color: #252631;
}
</style>
