<template>
  <div class="app-container">
    <div class="app-container-search main-bgcolor main-radius">
      <h1 class="">供应商管理</h1>
      <br />
      <!-- 搜索工作栏 -->
      <SearchInfo
        ref="searchForm"
        :searchFormParam="searchFormParam"
        :showSearch.sync="showSearch"
        @handleQuery="handleQuery"
        @resetQuery="resetQuery"
      ></SearchInfo>
    </div>

    <div class="app-container-list main-bgcolor main-radius shopping">
      <div class="shopping-top">
        <div class="shopping-title">供应商列表</div>
        <el-button class="shopping-add-btn" type="primary" plain @click="handleAdd"
          v-hasPermi="['basic:vendor-info:create']">添加供应商</el-button>
      </div>

      <!-- 列表 -->
      <el-table v-loading="loading" :data="list">
        <el-table-column label="编号" align="center" type="index" width="80" >
          <template v-slot="{ $index }">
            <span>{{($index+1)+ (searchFormParam.queryParams.pageNo-1)*searchFormParam.queryParams.pageSize }}</span>
          </template>
        </el-table-column>
        <el-table-column label="供应商名称" align="center" prop="vendorName"/>
        <el-table-column label="供应商类型" align="center" prop="vendorType" >
          <template v-slot="scope">
            <template v-if="scope.row.vendorType=='0'">企业</template>
            <template v-if="scope.row.vendorType=='1'">个人</template>
          </template>
        </el-table-column>
        <el-table-column label="所属商户" align="center" prop="belongBusinessName" />
        <el-table-column label="备注" align="center" prop="remarks" :show-overflow-tooltip="true"/>
        <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
          <template v-slot="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                      v-hasPermi="['basic:vendor-info:update']">编辑</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                      v-hasPermi="['basic:vendor-info:delete']">删除</el-button>
          </template>
        </el-table-column>
        <div slot="empty">
            <EmptyTable />
        </div>
      </el-table>
      <!-- 分页组件 -->
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="searchFormParam.queryParams.pageNo"
        :limit.sync="searchFormParam.queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="500px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" label-position="top">
        <el-form-item label="所属商户" prop="belongBusinessName">
          <div style="display: flex;">
            <el-select v-model="form.belongBusinessId" :placeholder=this.belongBusinessPlaceholder clearable filterable style="flex: 1;">
              <el-option v-for="businessInfo in businessNameList"
               :key="businessInfo.index" :label="businessInfo.businessName" :value="businessInfo.id" @click.native="labelClick(businessInfo.businessName)"/>
            </el-select>
          </div>
        </el-form-item>
        <el-form-item label="所属行政区" prop="area">
          <!--下拉框地区操作先财要求修改成输入框功能 <div style="display: flex;">
            <CascaderArea style="flex: 1;" :pca="areaList" :isValue="form.area" ref="cascaderArea" v-model="form.area" @transfer="getArea"></CascaderArea>
          </div> -->
          <el-input v-model="form.area" placeholder="请输入所属行政区地址" />
        </el-form-item>
        <el-form-item label="供应商类型" prop="vendorType">
          <el-radio-group v-model="form.vendorType" @change="vendorTypeChange">
            <el-radio :label="0">企业</el-radio>
            <el-radio :label="1">个人</el-radio>
          </el-radio-group>
        </el-form-item>

        <!-- 企业类型 -->
        <el-form-item label="供应商名称" prop="vendorName" v-if="form.vendorType==0">
          <el-input v-model="form.vendorName" placeholder="请输入供应商名称" />
        </el-form-item>
        <el-form-item label="统一信用代码" prop="businessCode" v-if="form.vendorType==0">
          <el-input v-model="form.businessCode" placeholder="请输入统一信用代码" />
        </el-form-item>
        <el-form-item label="联系人姓名" prop="contactName" v-if="form.vendorType==0">
          <el-input v-model="form.contactName" placeholder="请输入联系人姓名" />
        </el-form-item>
        <!-- 个人类型 -->
        <el-form-item label="姓名" prop="realName" v-if="form.vendorType==1">
          <el-input v-model="form.realName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="身份证" prop="idCard" v-if="form.vendorType==1">
          <el-input v-model="form.idCard" placeholder="请输入身份证" />
        </el-form-item>

        <el-form-item label="联系电话" prop="contactPhone">
          <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { createVendorInfo, updateVendorInfo, deleteVendorInfo, getVendorInfo, getVendorInfoPage } from "@/api/basic/vendorInfo";
import { getBusinessNameList } from "@/api/basic/businessInfo";
import {getAreaTree} from "@/api/system/area";
import SearchInfo from "@/components/Customer/SearchForm"
import EmptyTable from '@/components/EmptyTable'
import CascaderArea from "@/components/Customer/Cascader/area"
export default {
  name: "VendorInfo",
  components: {
    SearchInfo,
    EmptyTable,
    CascaderArea,
  },
  data() {
    return {
      //所属商户下拉框提示
      belongBusinessPlaceholder:'请选择所属商户',
      //获取的地区信息列表
      areaList:[],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //头部查询表单组件的参娄
      searchFormParam:{
        // 查询参数
          queryParams: {
            pageNo: 1,
            pageSize: 10,
            vendorName: null,
            vendorType: null,
            belongBusinessName: null,
        },
        //表单对象
        dataSource:[
          {
            key:'vendorName',
            itemType:'input',
            label:'供应商名称',
            placeholder:'请输入',
            onEnterKeyUp:this.handleQuery
          },
          {
            key:'vendorType',
            itemType:'select',
            label:'供应商类型',
            placeholder:'请选择',
            options:[
              {
                label:'企业',
                value:'0',

              },
              {
                label:'个人',
                value:'1',

              },
            ],
          },
          {
            key:'belongBusinessName',
            itemType:'input',
            label:'所属商户',
            placeholder:'请输入',
            onEnterKeyUp:this.handleQuery
          },
        ],
      },
      //商户名称列表
      businessNameList:{},
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        vendorName: [{ required: true, message: "供应商名称不能为空", trigger: "blur" }],
        belongBusinessName: [{ required: true, message: "所属商户不能为空", trigger: "blur" }],
        belongBusinessId: [{ required: true, message: '所属商户不能为空', trigger: 'blur' }],
        area: [{ required: true, message: "所属行政区不能为空", trigger: "blur" }],
        vendorType: [{ required: true, message: "请选择供应商类型", trigger: "blur" }],
        realName: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
        idCard: [{
          required: true,
          message: "身份证不能为空",
          trigger: "blur",
        },{
          pattern: /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)/,
          message: '身份证号码格式有误！',
          trigger: 'blur',
        }],
        contactPhone: [{
          pattern:  /^1[3|4|5|7|8|9][0-9]{9}$/,
          message: '联系电话格式有误！',
          trigger: 'blur',
        }],
      }
    };
  },
  created() {
    this.getList();
    this.getAreaList();
  },
  activated() {
    this.getList();
  },
  methods: {
    /** 地区查询列表 */
    getAreaList() {
      getAreaTree().then(response => {
        this.areaList = response.data;
      })
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getVendorInfoPage(this.searchFormParam.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 获取当前分页列表 */
    getVendorInfoList() {
      this.loading = true;
      let queryParams = {
          pageNo: this.searchFormParam.queryParams.pageNo,
          pageSize: this.searchFormParam.queryParams.pageSize,
      }
      // 执行查询
      getVendorInfoPage(queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 获取商户名称列表 */
    getBusinessNameList(){
      getBusinessNameList().then(response => {
        this.businessNameList = response.data;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        vendorName: undefined,
        vendorId: undefined,
        vendorType: undefined,
        belongBusinessId: undefined,
        belongBusinessName: undefined,
        remarks: undefined,
        businessCode: undefined,
        contactPhone: undefined,
        contactName: undefined,
        area: undefined,
        realName: undefined,
        idCard: undefined,
      };
      this.resetForm("form");
    },
    /** 供应商类型选项变更，表单重置 */
    vendorTypeChange() {
      this.form.vendorName = undefined,
      this.form.remarks = undefined,
      this.form.businessCode = undefined,
      this.form.contactPhone = undefined,
      this.form.contactName = undefined,
      this.form.realName = undefined,
      this.form.idCard = undefined
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.searchFormParam.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm", "searchForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.belongBusinessPlaceholder = '请选择所属商户'
      this.getBusinessNameList();
      this.open = true;
      this.title = "添加供应商";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      this.getBusinessNameList();
      getVendorInfo(id).then(response => {
        this.form = response.data;

        //当商户被删除时，无法显示对应的商户名称，重新定义
        let isTip = true;
        for(var x=0;x<this.businessNameList.length;x++){
          if(this.businessNameList[x].id == this.form.belongBusinessId){
            isTip = false;
          }
        }
        if( isTip ) {
          this.form.belongBusinessId = undefined
          this.form.belongBusinessName = undefined
          this.belongBusinessPlaceholder = '所属商户已删除，请重新选择'
        }else{
          this.belongBusinessPlaceholder = '请选择所属商户'
        }

        this.open = true;
        this.title = "修改供应商";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateVendorInfo(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getVendorInfoList();
          });
          return;
        }
        // 添加的提交
        createVendorInfo(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getVendorInfoList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除该供应商?').then(function() {
          return deleteVendorInfo(id);
        }).then(() => {
          this.getVendorInfoList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
    //父组件中获取地区组件传递参数方法
    getArea(val){
      this.form.area = val
    },
    //获取el-select选择的标签
    labelClick(data){
      this.form.belongBusinessName = data;
    },

  }
};
</script>
