<!--
    supply 供应合同选择
-->
<template>
  <el-dialog title="选择供应合同" :visible.sync="dialog.supply" 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}}</el-radio>
                </template>
              </el-table-column>
            </el-table>
          </el-radio-group>
          <div class="right" style="background: #fff;">
            <p style="height:40px;line-height:40px;text-align:center;color:#9E9E9E; float: left;">
                找不到想要的型号？ 赶快去
                <i style="color:#46B0FF;cursor: pointer;">厂家库</i>维护吧
            </p>
            <my-pagination :page="page" @change="pageChange" style="margin-top:10px;"></my-pagination>
        </div> 
          <div class="footer">
            <el-button
              type="primary"
              style="display:block;margin: 10px auto 0;"
              @click="setData"
            >确 定</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>
            <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 { getListForSupplier } from "@/api/scan/request"
import tools from "@/utils/scan/tools"
import { mapGetters, mapActions } from "vuex";

export default {
  components: {
    myPagination
  },
  props: {
    dialog: {
      type: Object,
      default: function () {
        return {
          supply: true
        };
      }
    },
    id: {
      type: [Number, String],
      default: 0
    }
  },
  computed: {
    ...mapGetters("layout", ["projectId", "secret", "userId"])
  },
  data() {
    return {
      search: "", //搜索文案
      radio: "",
      formData: {
        name: ""
      },
      rules: {
        name: { required: true, message: '请输入保单号', trigger: 'blur' },
      },
      tableData: [],
      page: {
        size: 50,
        sizes: [10, 30, 50, 100, 150, 200],
        total: 0,
        currentPage: 1
      },
      allData: []
    };
  },
  created() { },
  mounted() { },
  methods: {
    submitForm(formName) {
      let _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          _this.$emit("change", this.formData.name)
          this.dialog.supply = false
        } else {
          return false;
        }
      });
    },
    resetForm() {
      this.$refs.ruleForm.resetFields();
    },

    setData() {
      this.$emit("change", this.radio)
      this.dialog.supply = false
    },

    //获取列表
    getList() {
      this.radio = ""
      let _this = this
      this.page = {
        size: 50,
        sizes: [10, 30, 50, 100, 150, 200],
        total: 0,
        currentPage: 1
      }
      if (!!this.id) {
        let param = {
          data: {
            projectId: this.projectId,
            venderId: this.id
          },
          ProjId: this.projectId,
          secret: this.secret,
        }
        getListForSupplier(param, res => {
          _this.$refs.ruleForm.resetFields()
          _this.allData = res.contractId || []
          _this.page.total = _this.allData.length
          //分页
          _this.pageChange()
        })
      }
    },
    pageChange() {
      this.tableData = tools.pagination(this.page.currentPage, this.page.size, this.allData)
    }
  },
  watch: {
    dialog: {
      deep: true,
      handler: function (val) {
        if (this.dialog.supply) {
          this.getList()
        }
      }
    }
  }
};
</script>
