<template>
  <div>
    <div class="sty">
      <el-page-header content="详情" @back="goBack" />
    </div>
    <el-form :model="infoList" label-width="130px" size="small" label-position="right">
      <el-card class="">
        <div slot="header" class="clearfix">
          <strong>客户</strong>
        </div>
        <div>
          <!-- <el-form label-width="130px" size="small" label-position="right"> -->
          <el-row :gutter="20">
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="创建组织" prop="">
                <el-input />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="客户编码" prop="">
                <el-input v-model="infoList.number" />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="使用组织" prop="">
                <el-input v-model="infoList.useOrgName" />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="简称" prop="">
                <el-input />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- </el-form> -->
        </div>
      </el-card>
      <br>
      <el-card class="">
        <div slot="header" class="clearfix">
          <strong>基本信息</strong>
        </div>
        <span>地址</span>
        <el-divider />
        <div>
          <!-- <el-form label-width="130px" size="small" label-position="right"> -->
          <el-row :gutter="20">
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="国家" prop="">
                <el-input />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="地区" prop="">
                <el-input v-model="infoList.region" />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="省份" prop="">
                <el-input v-model="infoList.province" />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="城市" prop="">
                <el-input v-model="infoList.city" />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="通信地址" prop="">
                <el-input />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="邮政编码" prop="">
                <el-input />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="公司网址" prop="">
                <el-input />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="商城订单对应组织" prop="">
                <el-input />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="联系邮箱" prop="">
                <el-input v-model="infoList.email" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- </el-form> -->
        </div>
        <span>管理</span>
        <el-divider />
        <div>
          <!-- <el-form label-width="130px" size="small" label-position="right"> -->
          <el-row :gutter="20">
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="联系电话" prop="">
                <el-input v-model="infoList.mobile" />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="传真" prop="">
                <el-input />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="公司类别" prop="">
                <el-input />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="公司性质" prop="">
                <el-input />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="公司规模" prop="">
                <el-input />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- </el-form> -->
        </div>
        <span>相关</span>
        <el-divider />
        <div>
          <!-- <el-form label-width="130px" size="small" label-position="right"> -->
          <el-row :gutter="20">
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="对应供应商" prop="">
                <el-input />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="对应集团客户" prop="">
                <el-input />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="客户类别" prop="">
                <el-input />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="客户分组" prop="">
                <el-input />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="集团客户" prop="">
                <el-checkbox />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="默认付款方" prop="">
                <el-checkbox />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="不校验可发量" prop="">
                <el-checkbox />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="对应组织" prop="">
                <el-input />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="备注" prop="">
                <el-input />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- </el-form> -->
        </div>
        <span>开票信息</span>
        <el-divider />
        <div>
          <!-- <el-form label-width="130px" size="small" label-position="right"> -->
          <el-row :gutter="20">
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="发票抬头" prop="">
                <el-input />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="纳税登记号" prop="">
                <el-input />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="开户银行" prop="">
                <el-input v-model="infoList.bank" />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="银行账号" prop="">
                <el-input v-model="infoList.bankAccount" />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="开票联系电话" prop="">
                <el-input v-model="infoList.invoicePhone" />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="开票通讯地址" prop="">
                <el-input v-model="infoList.invoiceAddress" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- </el-form> -->
        </div>
        <span>智慧订货</span>
        <el-divider />
        <div>
          <!-- <el-form label-width="130px" size="small" label-position="right"> -->
          <el-row :gutter="20">
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="允许对接智慧订单" prop="">
                <el-checkbox />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- </el-form> -->
        </div>
      </el-card>
    </el-form>
    <br>
    <el-card>
      <div slot="header" class="clearfix">
        <strong>信用额度</strong>
      </div>
      <div class="diy-table-1">
        <el-row :gutter="0" class="xyed">
          <div v-for="(v, i) in infoList.customerWallets" v-show="v.type == 'COMMONLY'" :key="i">
            <el-col :span="12" class="item">
              <div class="label">信用额度</div>
              <div class="value">{{ v.freeCreditAmount }}</div>
            </el-col>
            <el-col :span="12" class="item">
              <div class="label">钱包类型</div>
              <div class="value">{{ v.customerWalletName }}-{{ v.type == 'COMMONLY' ? '普通钱包' : '返利钱包' }}</div>
            </el-col>
          </div>
        </el-row>
      </div>
    </el-card>
    <br>
    <el-card>
      <div slot="header" class="clearfix">
        <strong>标签信息</strong>
      </div>
      <div class="diy-table-1" style="width: 100%">
        <el-form label-width="120px" class="demo-ruleForm">
          <el-row>
            <el-col :span="6">
              <el-form-item label="集团客户" prop="">
                <el-radio v-model="infoList.isGroupCompany" :label="true">是</el-radio>
                <el-radio v-model="infoList.isGroupCompany" :label="false">否</el-radio>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="是否可转折让" prop="">
                <el-radio v-model="infoList.isZr" :label="true">是</el-radio>
                <el-radio v-model="infoList.isZr" :label="false">否</el-radio>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label-width="0px"> 注：设置后返利可以转入折让钱包 </el-form-item>
            </el-col>
            <!-- <el-col :span="12">
              <el-form-item label="是否前置仓" prop="">
                <el-radio v-model="infoList.isFront" :label="true">前置仓</el-radio>
                <el-radio v-model="infoList.isFront" :label="false">经销商自有仓库</el-radio>
                <el-button type="primary" size="mini" @click="handelSubmit">修改</el-button>
              </el-form-item>
            </el-col> -->
          </el-row>
          <el-col>
            <el-form-item label="分子公司">
              <el-tag v-for="v in infoList.kingDeeCustomers" :key="v.id" class="tagStyle">{{ v.name }}</el-tag>
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="提货方式">
              <div class="pick">
                <el-row v-if="dataList.length">
                  <el-col v-for="(item, index) in dataList" :key="index" :span="24" class="pick-item">
                    <el-checkbox-group v-model="categoryArr">
                      <el-col :span="6" class="pick-item-category">
                        <el-checkbox :label="item.id">
                          {{ item.name }}
                        </el-checkbox>
                      </el-col>
                    </el-checkbox-group>
                    <el-col :span="18" class="pick-item-way">
                      <el-checkbox v-model="item.isFront" :label="true"> 物流配送(嘉贤物流)</el-checkbox>
                      <el-checkbox v-model="item.isPick" :label="true">商家自提</el-checkbox>
                    </el-col>
                  </el-col>
                </el-row>
              </div>
              <div>注：设置配送方式后提货预约可根据配置的提货方式进行选择</div>
              <div style="margin: 20px 0">
                <el-button type="primary" size="default" @click="onSubmit">保存</el-button>
              </div>

              <!-- <el-col :span="12">
                <div>
                  <el-checkbox v-model="isFront" :label="true"> 物流配送(嘉贤物流)</el-checkbox>
                </div>
                <div>选择商家自提需配置物流产品类别</div>
                <div>

                </div>
              </el-col>
              <el-col :span="12">
                <div>
                  <el-checkbox v-model="isPick" :label="true">商家自提</el-checkbox>
                </div>
                <div>选择商家自提需配置自提产品类别</div>
                <div>
                  <el-checkbox-group v-model="pickCategory">
                    <el-checkbox v-for="(item, index) in dataList" :key="index" :label="item.id">
                      {{ item.name }}
                    </el-checkbox>
                  </el-checkbox-group>
                </div>
              </el-col>
              <div>注：设置配送方式后提货预约可根据配置的提货方式进行选择</div>
              <div>
                <el-button type="primary" size="default" @click="onSubmit">保存</el-button>
              </div> -->
            </el-form-item>
          </el-col>
        </el-form>
      </div>
    </el-card>
    <br>
    <el-card>
      <div slot="header" class="clearfix">
        <strong>售后系统关联信息</strong>
      </div>
      <!-- type="flex" justify="center" align="middle" class="" -->
      <el-row class="flex">
        <el-col :span="2">售后系统编号</el-col>
        <el-col
          :span="3"
          style="margin: 0 20px"
        ><el-input v-model="infoList.afterNumber" placeholder="请输入售后系统编号" size="mini" clearable />
        </el-col>
        <el-col :span="4">售后系统绑定状态</el-col>
        <el-col :span="3">{{ infoList.binding === 'YES' ? '是' : '否' }}</el-col>
        <el-col :span="3">绑定时间</el-col>
        <el-col :span="3">{{ infoList.bindingTime }}</el-col>
        <el-col :span="3">绑定人</el-col>
        <el-col :span="2">{{ infoList.bindingName }}</el-col>
      </el-row>
      <div style="margin: 20px 0">
        <el-button type="primary" size="default" @click="onSave">保存</el-button>
      </div>
      <!-- <zj-table /> -->
    </el-card>
  </div>
</template>

<script>
import { getKingDeeCategory } from '@/api/basic_data/material'
import { getDealerInfo, updateCustomer, updatePick, updateSystem } from '@/api/basic_data/dealer'

export default {
  props: {
    detailsId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      infoList: {},
      dataList: [],
      radio: [],
      isFront: false,
      isPick: false,
      pickCategory: [],
      frontCategory: [],
      categoryArr: [],
      afterNumber: ''
    }
  },

  async created() {
    await this.getDataList()
    const res = await getDealerInfo({ id: this.detailsId })
    // this.isFront = res.data.isFront
    // this.isPick = res.data.isPick
    if (res.data.kingDeeCustomerCategoriesFront && res.data.kingDeeCustomerCategoriesFront.length) {
      this.frontCategory = res.data.kingDeeCustomerCategoriesFront.map(k => {
        return k.categoryId
      })
    }
    if (res.data.kingDeeCustomerCategories && res.data.kingDeeCustomerCategories.length) {
      this.pickCategory = res.data.kingDeeCustomerCategories.map(k => {
        return k.categoryId
      })
    }
    this.frontCategory.forEach(k => {
      this.dataList.forEach(i => {
        if (i.id === k) {
          i.isFront = true
        }
      })
    })
    this.pickCategory.forEach(k => {
      this.dataList.forEach(i => {
        if (i.id === k) {
          i.isPick = true
        }
      })
    })
    this.categoryArr = [...new Set([...this.frontCategory, ...this.pickCategory])]
    this.infoList = res.data
  },
  methods: {
    goBack() {
      this.$emit('close')
    },
    // 金蝶存货类别列表
    async getDataList() {
      const res = await getKingDeeCategory()
      res.data.forEach(k => {
        k.isFront = false
        k.isPick = false
      })
      this.dataList = res.data
    },

    handelSubmit() {
      updateCustomer({
        id: this.infoList.id,
        isFront: this.infoList.isFront
      }).then(res => {
        this.$successMsg(this.infoList.isFront ? '已更改为前置仓' : '已更改为经销商自有仓库')
      })
    },
    onSubmit() {
      this.pickCategory = []
      this.frontCategory = []
      if (this.categoryArr.length) {
        for (let i = 0; i < this.dataList.length; i++) {
          const item = this.dataList[i]
          for (let j = 0; j < this.categoryArr.length; j++) {
            const category = this.categoryArr[j]
            if (category === item.id) {
              if (item.isFront) {
                this.frontCategory.push(category)
              }
              if (item.isPick) {
                this.pickCategory.push(category)
              }
            }
          }
        }
      } else {
        this.$errorMsg('必须选择产品类别')
        return
      }

      updatePick({
        id: this.detailsId,
        isFront: !!this.frontCategory.length,
        isPick: !!this.pickCategory.length,
        pickCategory: this.pickCategory.length ? this.pickCategory.join(',') : '',
        frontCategory: this.frontCategory.length ? this.frontCategory.join(',') : ''
      }).then(res => {
        this.$successMsg('保存成功')
        this.$emit('close')
      })
    },
    onSave() {
      updateSystem({
        id: this.detailsId,
        afterNumber: this.afterNumber
      }).then(res => {
        getDealerInfo({ id: this.detailsId }).then(res => {
          const { afterType, bindingTime, bindingName } = res.data
          this.infoList.afterType = afterType
          this.infoList.bindingTime = bindingTime
          this.infoList.bindingName = bindingName
          this.$successMsg('保存成功')
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.sty {
  margin-bottom: 20px;
}

::v-deep .diy-table-1 {
  width: 50%;

  .xyed {
    border-top: 1px solid #ebeef5;

    .item {
      margin-bottom: 18px;
    }
  }
}
.pick {
  width: 600px;
  border: 1px solid #ebeef5;
  border-bottom: none;
  &-item {
    height: 40px;
    border-bottom: 1px solid #ebeef5;
    &-category {
      border-right: 1px solid #ebeef5;
    }
    &-category,
    &-way {
      padding: 0 20px;
    }
  }
}
.flex {
  display: flex;
  align-items: center;
}
.tagStyle {
  margin: 0 18px;
}
</style>
