<template>
  <div>
    <el-dialog title="修改服务门店" custom-class="customer-dailog" :visible.sync="dialogVisible"
      :close-on-click-modal="false" @close="cancel" width="600px">
      <el-form ref="form" :model="form" :rules="rules" label-width="133px">
        <el-form-item label="服务门店：" prop="mainStoreId">
          <el-select style="width: 386px" v-model="form.mainStoreId" filterable remote
            reserve-keyword placeholder="请输入门店名称/门店code" @change="getClerckList"
            :remote-method="getMainStoreCodeName" :loading="loading">
            <el-option v-for="item in mainstoreList" :key="item.storeId" :label="item.storeName"
              :value="item.storeId" />
          </el-select>
        </el-form-item>
        <el-form-item label="专属导购：" prop="mainClerkId">
          <el-select v-model="form.mainClerkId" style="width: 386px">
            <el-option v-for="item in clerkOptions" :key="item.clerkId" :label="item.clerkName"
              :value="item.clerkId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="原因备注：" prop="remark">
          <el-input type="text" maxlength="20" show-word-limit v-model="form.remark"
            style="width: 386px;"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" :loading="btnLoading" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { doFetch, doFetchqs, doFetchGet } from "../../axios/api";
import url from "../../axios/url";
import { mapState } from "vuex";
import { checkFalse, checkStatus, checkSuccess } from "../../../../static/js/checkStatus";
export default {
  name: "customermainstoreDialog",
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    mainStoreName: {
      type: String,
      default: ''
    },
    mainStoreId: {
      type: String,
      default: ''
    },
    mainClerkId: {
      type: String,
      default: ''
    },
  },
  data() {
    return {
      form: {
        mainStoreIdBak: '', // 修改前的服务门店id
        mainStoreId: '',
        mainClerkId: '',
        remark: '',
        optType: 1 // 1服务门店，2协管门店
      },
      rules: {
        mainStoreId: [
          { required: true, message: "请选择服务门店", trigger: "change" }
        ],
        mainClerkId: [
          { required: true, message: "请选择专属导购", trigger: "change" }
        ],
        remark: [
          { required: true, message: "请输入原因备注", trigger: "change" }
        ],
      },
      mainstoreList: [], // 服务门店列表
      clerkOptions: [], // 专属导购列表
      loading: false,
      btnLoading: false,
    };
  },
  computed: {
    ...mapState([ "showEditClique" ])
  },
  methods: {
    cancel() {
      this.$refs[ 'form' ].resetFields();
      this.$emit('update:dialogVisible', false);
    },
    // 服务门店列表
    getMainStoreCodeName(val) {
      this.loading = true;
      doFetchqs(url.storeCodeName, {
        searchParam: val,
        flag: 1
      })
        .then(res => {
          if (res.data.errorCode === 0) {
            this.mainstoreList = res.data.result;
          } else {
            checkFalse(res.data.message);
          }
        })
        .catch(err => {
          checkStatus(err);
        }).finally(_ => this.loading = false);
    },
    // 专属导购列表
    getClerckList(v, type) {
      if (!type) this.form.mainClerkId = '';
      this.clerkOptions = [];
      doFetchGet(url.getClerkList, {
        storeId: v,
        requestProject: "member"
      }).then(res => {
        const { errorCode, message, result = [] } = res.data || {};
        if (errorCode != 0) return this.$message.error(message);
        this.clerkOptions = result;
      });
    },
    submit() {
      this.$refs[ 'form' ].validate(valid => {
        if (valid) {
          this.btnLoading = true;
          doFetchGet(url.updateMemberStore, Object.assign({}, this.form))
            .then(res => {
              if (res.data.errorCode === 0) {
                setTimeout(() => {
                  this.btnLoading = false;
                  checkSuccess();
                  this.$refs[ 'form' ].resetFields();
                  this.$emit('update:dialogVisible', false);
                  this.$emit('refresh');
                }, 4500);
              } else {
                checkFalse();
                this.btnLoading = false;
              }
            })
            .catch(err => {
              this.btnLoading = false;
              checkStatus(err);
            });
        } else {
          return false;
        }
      });
    },
  },
  watch: {
    dialogVisible(n, o) {
      if (n) {
        this.form = Object.assign({}, this.form, { mainStoreIdBak: this.mainStoreId, mainStoreId: this.mainStoreId, mainClerkId: this.mainClerkId });
        this.getMainStoreCodeName(this.mainStoreName);
        this.getClerckList(this.mainStoreId, true);
      }
    }
  }
};

</script>
<style lang="less">
// 去除dailog-footer上边框
.customer-dailog .el-dialog__footer {
  padding-top: 0;
  padding-bottom: 20px;
  border-top: none !important;
}
.customer-dailog .el-dialog__body {
  padding-bottom: 0;
}
</style>