<!--
  Copyright (c) 2020.
  author: song
  email: 15235360226@163.com
  -->

<template>
  <div class="inner">
    <bread-crumb :arr="arr" class="pb-5 mb-5" />
    <div class="white pa-5 mb-5 d-flex flex-nowrap align-center justify-space-between">
      <div>
        <span class="font-16 textColor">联系人：</span>
        <a-input style="width:auto" v-model="name" />
      </div>
      <div>
        <span class="font-16 textColor">联系电话：</span>
        <a-input @change="onChange" style="width:auto" v-model="phone" />
      </div>
      <div>
        <span class="font-16 textColor">公司名称：</span>
        <a-input style="width:200px" v-model="company" />
      </div>
    </div>
    <div class="cart-box" v-if="cart.lists && cart.lists.length>0">
      <div
        :key="list.id"
        class="d-flex flex-column pa-5 white div-box mb-8"
        v-for="list in cart.lists"
      >
        <p class="font-bold-20 title-color">{{ list.title }}</p>
        <div
          :key="index"
          class="d-flex flex-nowrap flex-row mb-8"
          v-for="(item, index) in list.list"
        >
          <img :src="item.src" class="cart-pic mr-8" />
          <div class="flex-1 flex-column">
            <p class="font-bold-20 title-color">{{ item.title }}</p>
            <p class="font-14 textColor">{{ item.content }}</p>
          </div>
        </div>
      </div>
      <div class="d-flex flex-row-reverse pb-10">
        <a-button @click="handleSubmit" type="primary">提交</a-button>
        <cart-model
          :phone="phone"
          :show-model="showModel"
          @handleCancel="handleCancel"
          @submitCart="submitCart"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations, mapActions } from 'vuex';
import BreadCrumb from 'components/BreadCrumb/BreadCrumb.vue';
import CartModel from './CartModel.vue';
import { searchCar, carAndBuy } from 'config/api';

export default {
  name: 'Cart',
  components: { BreadCrumb, CartModel },
  data() {
    return {
      arr: [{ name: '购物车', url: '' }],
      name: '',
      phone: '',
      company: '',
      cart: {
        // id: 1,
        // lists: [
        //   {
        //     id: 1,
        //     title: '服务',
        //     list: [
        //       {
        //         src: 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2059927486,2456161292&fm=26&gp=0.jpg',
        //         title: '检验检测服务',
        //         content: '用户填写的需求描述',
        //       },
        //       {
        //         src: 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2059927486,2456161292&fm=26&gp=0.jpg',
        //         title: '检验检测服务',
        //         content: '用户填写的需求描述',
        //       },
        //     ],
        //   },
        //   {
        //     id: 2,
        //     title: '设备',
        //     list: [
        //       {
        //         src: 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2059927486,2456161292&fm=26&gp=0.jpg',
        //         title: '检验检测服务',
        //         content: '用户填写的需求描述',
        //       },
        //       {
        //         src: 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2059927486,2456161292&fm=26&gp=0.jpg',
        //         title: '检验检测服务',
        //         content: '用户填写的需求描述',
        //       },
        //     ],
        //   },
        // ],
      },
      showModel: false,
      phoneRules: [
        { required: true, pattern: new RegExp(/^[1][3,4,5,6,7,8,9][0-9]{9}$/), whitespace: true, message: '请输入正确的手机号' },
      ],
    };
  },

  computed: mapState('home', ['userSer']),

  async created() {
    await this.getData();
    await this.getUserSer();
    if (this.userSer) {
      if (this.userSer.name) {
        this.name = this.userSer.name;
      }
      if (this.userSer.phone) {
        this.phone = this.userSer.phone;
      }
      if (this.userSer.companyName) {
        this.company = this.userSer.companyName;
      }
    }
  },

  methods: {
    ...mapMutations('home', []),
    ...mapActions('home', ['getUserSer']),

    // 获取购物车列表
    async getData() {
      try {
        const params = { param: { pageNum: 1, pageSize: 10 } };
        const res = await searchCar(params);
        const { data, msg, code } = res.data;
        if (code === 200) {
          this.cart = data;
        }
      } catch (error) {
        console.log(error);
      }
    },

    // 电话变化
    onChange(e) {
      this.phone = e.target.value;
    },

    handleSubmit() {
      if (this.userSer.phone !== this.phone) {
        this.openModel();
      } else {
        this.submitCart();
      }
    },

    // 提交
    async submitCart(tel) {
      try {
        if (tel) {
          this.phone = tel;
        }
        const { name, phone, company, cart } = this;
        const ids = [];
        ids.push(cart.id);
        const params = {
          param: {
            contactName: company,
            contactPhone: phone,
            companyName: name,
            ids,
          },
        };
        const res = await carAndBuy(params);
        const { data, msg, code } = res.data;
        if (code === 200) {
          this.$message.success('提交成功');
          this.confirmLoading = false;
        } else {
          throw msg;
          this.confirmLoading = false;
        }
      } catch (error) {
        this.$message.error(error);
        this.confirmLoading = false;
      }
    },

    // 打开弹窗
    openModel() {
      this.showModel = true;
    },

    // 关闭弹窗
    handleCancel() {
      this.showModel = false;
    },
  },
};
</script>

<style scoped lang="stylus">
.inner {
  margin: 40px auto;
}

.cart-box {
  .div-box {
    box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.16);
  }

  .cart-pic {
    width: 233px;
    height: 128px;
  }
}
</style>
