<!--
 * @Descripttion: (配送模板/tb_delivery 详情弹窗)
 * @version: (1.0)
 * @Author: (黎文豪)
 * @Date: (2023-06-16)
 * @LastEditors: (黎文豪)
 * @LastEditTime: (2023-06-16)
-->
<template>
  <el-dialog v-model="props.modelValue" title="配送模板信息详情" width="1250px" @closed="closeDialog" @open="openDialog">
    <el-form ref="formRef" :model="formData" :disabled="true">


      <el-row >
        <el-col v-if="userid == 1" :lg="12">
          <el-form-item :label-width="labelWidth" label="店铺" prop="shopGuid">
            <el-input v-model='formData.shopName' disabled type="text">
            </el-input>
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="模板名称" prop="deliveryName">
            <el-input v-model="formData.deliveryName" placeholder="请输入模板名称" />
          </el-form-item>
        </el-col>
        <el-col :lg="24">
          <el-form-item :label-width="labelWidth" label="计费方式" prop="deliveryBillingMethod">
            <el-radio-group v-model="formData.deliveryBillingMethod">
              <el-radio v-for="item in  charging_methods " :key="item.dictValue" :label="parseInt(item.dictValue)"
                >{{
                  item.dictLabel }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>

        <el-col :lg="24">
          <el-form-item :label-width="labelWidth + 20" label="配送区域及运费" prop="">
            <el-table :data="deliveryRuleDataList" border style="width: 100%">
              <el-table-column prop="deliveryRuleRegionText" label="可配送区域" width="350">
                <template #default="scope">
                  <el-row style="width: 100%">
                    <el-col :span="24">
                      <div class="mr-5">{{ scope.row.deliveryRuleRegionText }}</div>
                    </el-col>
                  </el-row>
                </template>
              </el-table-column>
              <el-table-column prop="deliveryRuleFirst" :label=deliveryRuleFirstTitle>
                <template #default="scope">
                  <el-input-number v-if="formData.deliveryBillingMethod == 1" v-model.number="scope.row.deliveryRuleFirst"
                    controls-position="right" :min="0" />
                  <el-input-number v-else v-model.number="scope.row.deliveryRuleFirst" controls-position="right" :min="0"
                    :precision="2" />
                </template>
              </el-table-column>
              <el-table-column prop="deliveryRuleFirstFee" label="	运费(元)">
                <template #default="scope">
                  <el-input-number v-model.number="scope.row.deliveryRuleFirstFee" controls-position="right" :min="0"
                    :precision="2" />
                </template>
              </el-table-column>
              <el-table-column prop="deliveryRuleAdditional" :label=deliveryRuleAdditionalTitle>
                <template #default="scope">
                  <el-input-number v-if="formData.deliveryBillingMethod == 1"
                    v-model.number="scope.row.deliveryRuleAdditional" controls-position="right" :min="0" />
                  <el-input-number v-else v-model.number="scope.row.deliveryRuleAdditional" controls-position="right"
                    :min="0" :precision="2" />
                </template>
              </el-table-column>
              <el-table-column prop="deliveryRuleAdditionalFee" label="续费(元)">
                <template #default="scope">
                  <el-input-number v-model.number="scope.row.deliveryRuleAdditionalFee" controls-position="right" :min="0"
                    :precision="2" />
                </template>
              </el-table-column>
            </el-table>
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="排序" prop="deliverySort">
            <el-input-number v-model.number="formData.deliverySort" controls-position="right" :min="0" />
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
  </el-dialog>
</template>

<script setup>
import { ElMessage } from 'element-plus'
import { reactive, ref, watch } from "vue";
import { getDeliveryRuleList } from "@/api/business/LogisticsManage/Deliverys/delivery.js";
import useUserStore from '@/store/modules/user'


// 打开弹窗时回调
const openDialog = async () => {
  await getcharging_methods()
  await getdeliveryRuleDataListFun()

}

const formData = ref({
  ...props.data,
});
watch(props, async (v) => {
  formData.value = v.data;
});

// -业务参数
// 计费方式字典选项列表
const charging_methods = ref([]);

const userid = useUserStore().userId

// 选择配送区域弹窗参数
const DeliveryRuleDialogVisible = ref(false);
const DeliveryRuleDialogRow = ref({});

// 配送区域和运费参数
const deliveryRuleFirstTitle = ref("首件 (个)")
const deliveryRuleAdditionalTitle = ref("续件 (个)")
const deliveryRuleDataList = ref([])


// -业务方法
// 字典获取
async function getcharging_methods() {
  await proxy.getDicts('charging_methods').then((res) => {
    charging_methods.value = res.data
  })
}

// 获取配送模板的配送区域和运费列表
async function getdeliveryRuleDataListFun() {
  getDeliveryRuleList({ deliveryGuid: formData.value.deliveryGuid }).then((res) => {
    if (res.code == 200) {
      deliveryRuleDataList.value = res.data
      deliveryRuleDataList.value.map((item) => {
        item.deliveryRuleRegion = item.deliveryRuleRegion == null ? [] : item.deliveryRuleRegion
      })
    }
  })
}



// 基础参数
const formRef = ref();
const labelWidth = 100;
const { proxy } = getCurrentInstance()
const props = defineProps({
  modelValue: Boolean,
  data: Object,
  done: Function,
});
const emits = defineEmits(["update:modelValue"]);


// -基础方法
const closeDialog = () => {
  emits("update:modelValue", false);
};

</script>
