<!--
    guarantee 保单选择
-->

<!--
    guarantee 供应合同选择
-->
<template>
  <el-dialog title="选择保单" :visible.sync="dialog.guarantee" width="600px">
    <el-tabs type="border-card">
      <el-tab-pane>
        <span slot="label">选择保单</span>
        <div id="firm">
          <el-radio-group v-model="radio" style="width:100%;">
            <el-table :data="tableData" style="width: 100%" height="300px">
              <el-table-column label="保单号">
                <template slot-scope="scope">
                  <el-radio v-model="radio" :label="scope.row">{{scope.row.insuranceNo}}</el-radio>
                </template>
              </el-table-column>
              <el-table-column label="下载">
                <template slot-scope="scope">
                  <el-button type="text">下载</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-radio-group>
          <my-pagination :page="page" style="margin-top:10px;"></my-pagination>
          <p style="height:40px;line-height:40px;text-align:center;color:#9E9E9E;">
            保单信息有错误？ 赶快去
            <i style="color:#46B0FF;cursor: pointer;">厂家库</i>维护吧
          </p>
          <div class="footer">
            <el-button
              type="primary"
              @click="getSelect"
              style="display:block;margin: 10px auto 0;"
            >确 定</el-button>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane>
        <span slot="label">新建保单</span>
        <el-form label-width="80px" :rules="rules" :model="formData" ref="ruleForm">
          <el-form-item label="保单号：" prop="name">
            <el-input v-model="formData.name"></el-input>
          </el-form-item>
          <el-form-item label="保险文件">
            <upload-files @change="changeFiles" ref="files" :keysArr="keysArr" :max="max"></upload-files>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
    </el-tabs>
  </el-dialog>
</template>
<script>
import myPagination from "@/components/ledger/lib/myPagination";
import uploadFiles from '@/components/ledger/lib/uploadFiles'
import { getListForGuarantee, createGuarantee } from "@/api/scan/request"
import tools from "@/utils/scan/tools"
export default {
  components: {
    myPagination,
    uploadFiles
  },
  props: {
    dialog: {
      type: Object,
      default: function () {
        return {
          guarantee: true
        };
      }
    },
    id: {
      type: [Number, String],
      default: 0
    }
  },
  data() {
    return {
      search: "", //搜索文案
      radio: "",
      formData: {
        name: ""
      },
      rules: {
        name: { required: true, message: '请输入保单号', trigger: 'blur' },
      },
      tableData: [],
      page: {
        size: 10,
        sizes: [10, 30, 50, 100, 150, 200],
        total: 400,
        currentPage: 1
      },
      keysArr: [],
      max: 1,
      allData: []
    };
  },
  created() { },
  mounted() { },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.createGree()
        } else {
          return false;
        }
      });
    },
    changeFiles(val) {
      this.keysArr = val
    },
    resetForm() {
      this.$refs.ruleForm.resetFields();
    },
    //新建保单
    createGree() {
      let param = {
        data: {
          venderId: this.id,
          insuranceNo: this.formData.name,
          projectId: this.$route.query.projId,
          contractFile: this.keysArr[0]
        }
      }
      let _this = this
      createGuarantee(param, res => {
        _this.$emit("change", param.data)
        _this.dialog.guarantee = false
      })
    },
    getList() {
      this.radio = ""
      let _this = this
      this.page = {
        size: 10,
        sizes: [10, 30, 50, 100, 150, 200],
        total: 0,
        currentPage: 1
      }
      if (!!this.id) {
        let param = {
          data: {
            projectId: this.$route.query.projId,
            venderId: this.id
          }
        }
        getListForGuarantee(param, res => {
          _this.$refs.ruleForm.resetFields()
          _this.$refs.files.resetFile()
          _this.allData = res.content || []
          _this.page.total = _this.allData.length
          //   //分页
          _this.pageChange()
        })
      }
    },

    getSelect() {
      this.$emit("change", this.radio)
      this.dialog.guarantee = false
    },
    pageChange() {
      this.tableData = tools.pagination(this.page.currentPage, this.page.size, this.allData)
    }
  },
  watch: {
    dialog: {
      deep: true,
      handler: function (val) {
        if (this.dialog.guarantee) {
          this.getList()
        }
      }
    }
  }

};
</script>
