<template>
  <div id="form-1">
    <!-- 团购订单申请售后 -->
    <el-row>
      <el-col
        :span="24"
        class="main animate__animated animate__backInRight"
        v-loading="loadings"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
      >
        <el-col class="top-btn">
          <el-button type="primary" size="mini" @click="toBack()">返回</el-button>
        </el-col>
        <el-col :span="9" class="one">
          <el-col :span="24" class="add">
            <el-col :span="2">
              <i class="el-icon-location"></i>
            </el-col>
            <el-col :span="22">
              <el-col :span="24"> {{ address.name }},{{ address.phone }} </el-col>
              <el-col :span="24"> {{ address.province }} , {{ address.city }} , {{ address.area }} , {{ address.address }} </el-col>
            </el-col>
          </el-col>
          <el-col :span="24" class="shop"> <i class="el-icon-s-shop"></i>{{ shop.name }}</el-col>
          <el-col :span="24">
            <el-col :span="24" class="goods bode">
              <el-col :span="6" class="image"><el-image :src="getFile()"></el-image></el-col>
              <el-col :span="18">
                <el-col :span="12">
                  <el-col :span="24"> {{ goods.name }} </el-col>
                  <el-col :span="24"> 规格：{{ spec.name }} </el-col>
                </el-col>
                <el-col :span="12" class="money">
                  <el-col :span="24" style="color: red; font-size: 20px">
                    <p>￥{{ spec.price }}</p>
                  </el-col>
                  <el-col :span="24"> X{{ spec.num }} </el-col>
                  <el-col :span="24">
                    <span class="spanfour" style="color: blue; cursor: pointer" @click="toUrl(goods.url)"> 查看商品来源:{{ goods.source }}</span>
                  </el-col>
                  <el-col :span="24"> {{ info.is_afterSale == true ? '已申请售后' : '未申请售后' }} </el-col>
                </el-col>
              </el-col>
            </el-col>
          </el-col>
          <el-col :span="24" class="bode">
            <el-col :span="6">下单时间</el-col>
            <el-col :span="18" class="other">{{ info.buy_time }}</el-col>
          </el-col>
          <el-col :span="24" class="bode">
            <el-col :span="6">订单备注</el-col>
            <el-col :span="18" class="other">{{ info.remarks || '' }}</el-col>
          </el-col>
          <el-col :span="24" class="bode">
            <el-col :span="6">配送方式</el-col>
            <el-col :span="18" class="other">{{ info.transport_type_name || '待填写' }}</el-col>
          </el-col>
          <el-col :span="24" class="goods_total bode">
            <el-col :span="24" v-for="(item, index) in total_detail" :key="index">
              <el-col :span="8">{{ item.zh }}</el-col>
              <el-col :span="16" class="other">
                <p>￥{{ item.money }}</p>
              </el-col>
            </el-col>
          </el-col>
          <el-col :span="24" class="goods_total bode">
            <el-col :span="8">实付金额</el-col>
            <el-col :span="16" class="other">
              <p>￥{{ pay.pay_money }} 元</p>
            </el-col>
          </el-col>
          <el-col :span="24" v-for="(transport, index) in transport" :key="index">
            <el-col :span="24" class="bode" v-if="info.status == '3'">
              <el-col :span="6">寄出运单号</el-col>
              <el-col :span="18" class="other">{{ transport.shop_transport_no || '暂无单号' }}</el-col>
            </el-col>
            <el-col :span="24" class="bode" v-if="info.status == '3'">
              <el-col :span="6">寄出快递公司</el-col>
              <el-col :span="18" class="other">{{ transport.shop_transport_name || '暂无快递信息' }}</el-col>
            </el-col>
          </el-col>
        </el-col>
        <el-col :span="9" class="one">
          <el-button type="danger" @click="toSales('1')" v-if="this.status == '1'"> 取消订单 </el-button>
          <el-button type="danger" @click="toSales('2')" v-if="this.status == '2'"> 拒收 </el-button>
          <el-col v-if="this.status == '3'">
            <el-form :model="form" :rules="rules" ref="form" label-width="180px">
              <!-- <el-col :span="24">
                <el-form-item label="售后商品" prop="goods">
                  <el-select v-model="form.goods" clearable filterable placeholder="请选择售后商品" size="small" style="width: 100%" @change="goodsChange">
                    <el-option v-for="i in list" :key="i.goods.name" :label="i.goods.name" :value="i.id"> </el-option>
                  </el-select>
                </el-form-item>
              </el-col> -->
              <el-col :span="24">
                <el-form-item label="售后类型" prop="type">
                  <el-select v-model="form.type" clearable filterable placeholder="请选择售后类型" size="small" style="width: 100%" @change="typeChange">
                    <el-option v-for="i in typeList" :key="i.value" :label="i.label" :value="i.value"> </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="24" v-if="this.form.type == '1' || this.form.type == '2'">
                <el-form-item label="退款金额" prop="money">
                  <el-input v-model="form.money" placeholder="请输入退款金额" size="small" type="Number" @input="toMoney"></el-input>
                  <p>
                    退款金额不得超过<span style="color: red">{{ payMoney.payTotal || '' }}</span
                    >元
                    <span @click="allMoney" style="color: blue; margin: 0 0 0 10px">全部退款</span>
                  </p>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="申请理由" prop="reason">
                  <el-select v-model="form.reason" clearable filterable placeholder="请选择申请理由" size="small" style="width: 100%">
                    <el-option v-for="i in reasonList" :key="i.value" :label="i.label" :value="i.value"> </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="售后描述" prop="desc">
                  <el-input v-model="form.desc" placeholder="请输入售后描述" size="small" type="textarea"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="附件" prop="file">
                  <c-upload :url="url" v-model="form.file" :limit="6"></c-upload>
                </el-form-item>
              </el-col>
              <el-col :span="24" class="btn">
                <el-button type="primary" size="mini" @click="onSubmit('form')">提交</el-button>
                <el-button type="danger" size="mini" @click="toBack()">取消</el-button>
              </el-col>
            </el-form>
          </el-col>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
const _ = require('lodash');
const moment = require('moment');

import { mapState, mapGetters, createNamespacedHelpers } from 'vuex';
const { mapActions: groupOrder } = createNamespacedHelpers('groupOrder');
const { mapActions } = createNamespacedHelpers('groupAfterSale');
const { mapActions: dictData } = createNamespacedHelpers('dictData');
const { mapActions: sot } = createNamespacedHelpers('sot');

export default {
  name: 'form-1',
  props: { id: { type: String }, status: { type: String } },
  components: { cUpload: () => import('@/components/usual/c-upload.vue') },
  data: function () {
    return {
      info: {},
      goods: {},
      spec: {},
      // 地址
      address: {},
      // 实付金额
      total_detail: {},
      // 商铺
      shop: {},
      // 运单号
      transport: [],
      pay: {},
      // 商品列表
      list: [],
      // 物流
      reverse: false,
      activities: [],
      activit: {},
      // 订单状态
      order_processList: [],
      // 快递公司
      shop_transport_typeList: [],
      loading: false,
      rules: {
        goods: [{ required: true, message: '请选择商品', trigger: 'change' }],
        type: [{ required: true, message: '请选择售后类型', trigger: 'change' }],
        money: [{ required: true, message: '请输入金额', trigger: 'blur' }],
      },
      // 售后表单
      form: {},
      // 售后类型
      typeList: [],
      // 售后申请理由
      reasonList: [],
      // 退款金额
      payMoney: '',
      url: '/files/point/afterSale/upload',
      loadings: true,
    };
  },
  async created() {
    await this.searchOther();
    await this.search();
  },
  methods: {
    ...dictData({ dictQuery: 'query' }),
    ...groupOrder({ orderFetch: 'fetch' }),
    ...sot({ sotFetch: 'fetch' }),
    ...mapActions(['query', 'cgfr', 'fetch', 'create', 'update']),
    // 查询
    async search() {
      let res;
      res = await this.orderFetch(this.id);
      if (this.$checkRes(res)) {
        this.$set(this, `address`, res.data.address);
        this.$set(this, `shop`, res.data.shop);
        this.$set(this, `goods`, res.data.goods);
        this.$set(this, `spec`, res.data.goodsSpec);
        this.$set(this, `pay`, res.data.pay);
        this.$set(this, `total_detail`, res.data.total_detail);
        this.$set(this, `info`, res.data);
        if (res.data.transport && res.data.transport.length > 0) {
          for (const p1 of res.data.transport) {
            let arr = await this.dictQuery({ code: 'transport_company', value: p1.shop_transport_type });
            if (this.$checkRes(arr)) {
              let type = arr.data.find((i) => i.value == p1.shop_transport_type);
              if (type) {
                p1.shop_transport_name = type.label;
                this.querySearch(type.label);
              }
            }
          }
          this.$set(this, `transport`, res.data.transport);
        }
      }
      this.loadings = false;
    },
    async querySearch(value) {
      let res = await this.dictQuery({ code: 'transport_company', label: value });
      if (this.$checkRes(res)) this.$set(this, 'shop_transport_typeList', res.data);
    },
    toUrl(url) {
      if (url) window.open(url, '_blank');
      else this.$message.error('该商品还未添加来源网址，无法跳转');
    },
    // 选择商品
    getFile(value) {
      let name;
      if (this.spec.file && this.spec.file.length != 0) name = this.spec.file[0].url;
      else if (this.goods.file && this.goods.file.length != 0) name = this.goods.file[0].url;
      return name;
    },
    handleSelect(value) {},
    async toSales(status) {
      if (status == '1') var info = { order: this.id, type: '4' };
      else if (status == '2') var info = { order: this.id, type: '5' };
      this.$confirm('是否确认申请售后', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        let res = await this.create(info);
        if (this.$checkRes(res)) {
          this.$message({ type: `success`, message: `申请售后成功` });
          this.search();
        }
      });
    },
    // 选择商品
    goodsChange(value) {
      this.$set(this.form, `goods`, value);
      this.typeChange(this.form.type || '');
    },
    // 选择类型
    async typeChange(value) {
      this.$set(this.form, `type`, value);
      if (value != '3') {
        this.$set(this, `payMoney`, this.pay.pay_money);
        this.allMoney();
      }
    },
    // 全部退款
    allMoney() {
      this.$set(this.form, `money`, this.payMoney);
    },
    // 输入金额
    toMoney(val) {
      if (val > this.payMoney) {
        this.$message.error('超过退款最大金额');
        this.$set(this.form, `money`, '');
      }
    },
    // 提交
    async onSubmit(formName, val) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          this.$confirm('是否确认保存', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          }).then(async () => {
            let form = this.form;
            form.order = this.id;
            form.shop = this.shop._id;
            let res = await this.create(form);
            if (this.$checkRes(res)) {
              this.$message({ type: `success`, message: `申请售后成功` });
              this.search();
            }
          });
        } else {
          return false;
        }
      });
    },
    // 返回
    toBack() {
      this.$emit('toBack');
    },
    // 查询其他信息
    async searchOther() {
      let res;
      // 减免方式
      res = await this.dictQuery({ code: 'order_process' });
      if (this.$checkRes(res)) this.$set(this, `order_processList`, res.data);
      // 售后类型
      res = await this.dictQuery({ code: 'afterSale_type' });
      if (this.$checkRes(res)) {
        let type = res.data.filter((i) => i.value != '4' && i.value != '5');
        this.$set(this, `typeList`, type);
      }
      // 售后申请理由
      res = await this.dictQuery({ code: 'afterSale_reason' });
      if (this.$checkRes(res)) {
        let type = res.data.filter((i) => i.value != '4' && i.value != '5');
        this.$set(this, `reasonList`, res.data);
      }
    },
  },
  computed: {},
  metaform() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  .one {
    margin: 10px 0 0 5%;
    padding: 5px;
    .add {
      border-bottom: 2px dashed #ccc;
      margin: 0 0 5px 0;
      padding: 5px 0;
    }
    .shop {
      padding: 4px 0;
      font-size: 18px;
      border-bottom: 1px solid #ccc;
    }
    .goods {
      padding: 10px 0;
      .image {
        padding: 0 10px 0 0;
      }
      .money {
        text-align: right;
      }
    }
    .other {
      text-align: right;
      word-break: break-all;
      p {
        color: red;
      }
      .spanfour {
        color: blue;
      }
    }
    .el-col {
      margin: 3px 0;
    }
    .bode {
      border-bottom: 1px solid rgb(238, 237, 237);
    }
    .form {
      margin: 20px 0 0 0;
    }
  }
}
.btn {
  text-align: center;
}

/deep/.el-timeline {
  margin: 20px 0 0 0;
  font-size: 16px;
}
/deep/.el-timeline-item__node:nth-child(1) {
  background-color: green;
}
</style>
