<template>
  <div>
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="60%" v-dialogDrag append-to-body>
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>车辆基本信息</span>
        </div>
        <el-descriptions class="margin-top" :column="3" border>
          <el-descriptions-item>
            <template slot="label">
              车牌号
            </template>
            {{formData.licenseNumber}}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              车辆品牌
            </template>
            <!-- <dict-tag :type="DICT_TYPE.DICT_SYS_USER_SEX" :value="formData.sex" /> -->
            {{formData.brandStr}}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              车辆型号
            </template>
            <!-- <dict-tag :type="DICT_TYPE.DICT_SYS_USER_SEX" :value="formData.sex" /> -->
            {{formData.modelStr}}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              车架号
            </template>
            {{formData.vin}}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              发动机号码
            </template>
            {{formData.engineNumber}}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              车辆注册日期
            </template>
            {{ parseTime(formData.carRegisterDate,'{y}-{m}-{d}') }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              车辆性质
            </template>
            <dict-tag :type="DICT_TYPE.DICT_CAR_NATURE" :value="formData.carNature" />
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              车辆类别
            </template>
            <dict-tag :type="DICT_TYPE.DICT_CAR_CATEGORY" :value="formData.carCategory" />
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              最近办理业务
            </template>
            <dict-tag :type="DICT_TYPE.DICT_CUS_BUSI_TYPE" :value="formData.recentlyHandledBusiness" />
          </el-descriptions-item>
        </el-descriptions>
      </el-card>

      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>客户信息</span>
          <el-button @click="openChooseCustomer" style="float: right; padding: 3px 0" type="text">选择客户</el-button>
        </div>
        <el-row>
          <el-empty v-if="formData.cusList.length == 0" description="暂无客户信息" :image-size="100"></el-empty>
          <el-col v-for="item in formData.cusList" :span="8" :key="item.id">
            <el-card shadow="hover">
              <el-descriptions>
                <template slot="title">
                  {{item.cusName}}({{ getDictDataLabel(DICT_TYPE.DICT_SYS_USER_SEX,item.sex) }})
                  <el-button @click="deleteCar(item)" style="text-align: right" size="mini" type="danger" icon="el-icon-delete" circle></el-button>
                </template>
                <el-descriptions-item label="车主"> <el-checkbox true-label="1" false-label="0" v-model="item.isOwner" /></el-descriptions-item>
                <el-descriptions-item label="联系电话">{{item.phoneNumber}}</el-descriptions-item>
              </el-descriptions>

            </el-card>
          </el-col>

        </el-row>
      </el-card>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
    <ChooseCustomerDraw @success="chooseCar"  ref="CustomerDraw"/>
  </div>
</template>

<script>
import * as CarMainApi from '@/api/base/carmain';
import ChooseCustomerDraw from '@/views/base/carmain/components/ChooseCustomerDraw.vue';
import { getDictDatas, DICT_TYPE, getDictData } from '@/utils/dict';

export default {
  name: "CustomerMainForm",
  components: {
    ChooseCustomerDraw
  },
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      //回参
      formData:{
        cusList:[],
      },

    };
  },
  methods: {

    getDictDataLabel(type,data){
      let resultObj = getDictData(type, data);

      return resultObj.label;
    },
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.formLoading = true;
      this.reset()
      try{
        const res = await CarMainApi.getCarMain(id);
        this.formData = res.data
        this.dialogTitle = "绑定客户";
      }finally {
        this.formLoading = false;
      }
    },
    /**删除卡片*/
    deleteCar(row){
      this.formData.cusList.forEach((item,index) => {
        if (item.id == row.id){
          this.formData.cusList.splice(index,1)
        }
      })
    },

    /**重置*/
    reset(){
      this.formData={
        cusList:[],
      }
    },

    openChooseCustomer(){
      this.$refs["CustomerDraw"].open(this.formData.cusList);
    },

    /**
     * 选择客户
     */
    chooseCar(chooseCuses){
      this.formData.cusList = chooseCuses
    },

    /** 提交按钮 */
    async submitForm() {
      this.formLoading = true
      try{
        const data = this.formData;
        await CarMainApi.bindCustomerCar(data);
        this.$modal.msgSuccess("绑定成功");
        this.dialogVisible = false;
        this.$emit('success');
      }finally {
        this.formLoading = false
      }
    },

  }
};
</script>
