<template>
  <div class="FSQStoreDetail">
    <div class="header">
      <van-nav-bar
        class="navBar"
        title="仿石漆服务商编辑"
        left-arrow
        @click-left="onClickLeft"
        right-text="保存"
        @click-right="clickSubmit" />
    </div>
    <div class="content">
      <van-form ref="tabstoreVal" @submit="onSubmit" :scroll-to-error="true" :show-error="false">
        <van-field readonly :value="fromData.storeCode" label="编号">
          <template #left-icon>
            <span class="van-f-red"></span>
          </template>
        </van-field>
        <van-field
          v-model="fromData.storeName"
          label="名称"
          placeholder="请输入名称"
          readonly
          :rules="[{ required: true, message: '请输入名称' }]">
          <template #left-icon>
            <span class="van-f-red"></span>
          </template>
        </van-field>
        <van-field
          readonly
          clickable
          name="picker"
          :value="fromData.storeCategoryName"
          label="类型"
          placeholder="点击选择类型">
          <template #left-icon>
            <span class="van-f-red"></span>
          </template>
        </van-field>
        <van-field
          v-model="fromData.orgName"
          label="销售部"
          placeholder="请输入销售部"
          readonly
          :rules="[{ required: true, message: '请输入销售部' }]">
          <template #left-icon>
            <span class="van-f-red"></span>
          </template>
        </van-field>
        <van-row style="border-bottom: 1px solid #ebedf0">
          <van-field
            rows="1"
            autosize
            type="textarea"
            readonly
            clickable
            name="picker"
            :value="fromData.chainName"
            label="经销商名称"
            placeholder="点击选择经销商名称"
            :rules="[
              {
                required: true,
                message: '请选择经销商',
              },
            ]">
            <template #left-icon>
              <span class="van-f-red"></span>
            </template>
          </van-field>
        </van-row>
        <van-field
          readonly
          clickable
          name="picker"
          :value="fromData.provinceName"
          label="省(州)"
          placeholder="点击选择省(州)">
          <template #left-icon>
            <span class="van-f-red"></span>
          </template>
        </van-field>
        <van-field
          readonly
          clickable
          name="picker"
          :value="fromData.cityName"
          label="城市"
          placeholder="点击选择城市">
          <template #left-icon>
            <span class="van-f-red"></span>
          </template>
        </van-field>
        <van-field
          readonly
          clickable
          name="picker"
          :value="fromData.districtName"
          label="区(县)"
          placeholder="点击选择区(县)">
          <template #left-icon>
            <span class="van-f-red"></span>
          </template>
        </van-field>
        <van-field
          readonly
          clickable
          name="picker"
          :value="fromData.townName"
          label="街道"
          placeholder="点击选择街道">
          <template #left-icon>
            <span class="van-f-red"></span>
          </template>
        </van-field>
        <van-field
          v-model="fromData.addressLine"
          label="地址"
          placeholder="请输入地址"
          rows="1"
          autosize
          type="textarea">
          <template #left-icon>
            <span class="van-f-red"></span>
          </template>
        </van-field>
        <van-field readonly v-model="fromData.contactName" label="实际经营人">
          <template #left-icon>
            <span class="van-f-red"></span>
          </template>
        </van-field>
        <van-field
          readonly
          type="tel"
          v-model="fromData.telephone"
          label="实际经营人电话"
          placeholder="请输入实际经营人电话"
          :rules="[
            {
              required: false,
              validator: FSQValidatePhone,
              message: '请输入手机号',
            },
          ]">
          <template #left-icon>
            <span class="van-f-red"></span>
          </template>
        </van-field>
        <van-field
          readonly
          v-model="fromData.salesmanName"
          label="所属销售员"
          placeholder="所属销售员">
          <template #left-icon>
            <span class="van-f-red"></span>
          </template>
        </van-field>
      </van-form>
    </div>
  </div>
</template>

<script>
import { getById, phoneCheck } from '@/api/index';
import { updateStore } from '@/api/FSQStore';
export default {
  data() {
    return {
      fromData: {
        storeCode: '',
        storeName: '',
        telephone: '',
        storeCategoryName: '',
        provinceName: '',
        cityName: '',
        districtName: '',
        addressLine: '',
        orgName: '',
        chainName: '',
        townName: '',
        contactName: '',
        salesmanName: '',
      },
    };
  },
  activated() {
    this.toastLoading(0, '加载中...', true);
    // 获取详情
    this.getDetail();
  },
  methods: {
    editorFn() {
      this.$router.push({
        path: '/addDesignerEdit',
        query: { id: this.$route.query.id },
      });
    },
    getDetail() {
      getById({ storeId: this.$route.query.id }).then((res) => {
        this.toastLoading().clear();
        if (res.code == 200) {
          this.fromData = res.data;
        }
      });
    },
    clickSubmit(values) {
      this.$refs.tabstoreVal.submit();
    },
    onSubmit(value) {
      this.toastLoading(0, '提交中...', true);
      this.fromData.storeId = this.$route.query.id;
      updateStore(this.fromData).then((res) => {
        this.toastLoading().clear();
        if (res.code == 200) {
          this.$dialog
            .confirm({
              title: '系统提示',
              message: '修改成功',
              showCancelButton: false,
            })
            .then(() => {
              this.onClickLeft();
            });
        }
      });
    },
    FSQValidatePhone(telephone) {
      return new Promise((resolve, reject) => {
        if (telephone == '') {
          resolve();
        }
        var telrg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
        if (telephone.trim() == '') {
          reject();
        } else if (!telrg.test(telephone)) {
          this.Toast('手机号格式错误');
          reject();
        } else {
          phoneCheck({ phoneNumber: telephone }).then((res) => {
            if (res.code == 200) {
              resolve();
            } else {
              this.Toast('手机号格式错误');
              reject();
            }
          });
        }
      });
    },
    onClickLeft() {
      this.$router.go(-1);
    },
  },
};
</script>
<style lang="scss" scoped>
.FSQStoreDetail {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  .header {
    height: 46px;
  }
  .content {
    flex: 1;
    overflow-y: auto;
  }
}
</style>
