<template>
  <div class="dialog" v-if="dialogForm">
    <div class="head_box">
      <span class="title">指令{{ title }} </span>
      <div>
        <el-button type="primary" size="small" :loading="loading" @click="submit" >保存</el-button>
        <el-button size="small" @click="close">关闭</el-button>
      </div>
    </div>
    <div class="form_cnt">
      <FormMessage v-model="listQuery" :formList="formData" ref="elform" :rules="rules">
        <template #glxsid>
          <div class="box pointer ellipsis" @click="chooseVisible = true">
            <span v-if="listQuery.glxs">{{ listQuery.glxs }}</span>
            <span v-else style="color: #b8b8b8;">请选择关联线索</span>
          </div>
        </template>
      </FormMessage>
    </div>
    <!-- 选择线索 -->
    <Xslist v-model="chooseVisible" @choosed="choosed" :roleIds="roleIds"></Xslist>
  </div>
</template>

<script setup>
import Xslist from '@/components/ChooseList/ChooseXs/index.vue'
import FormMessage from '@/components/aboutTable/FormMessage.vue'
import { qcckGet, qcckPost, qcckPut } from "@/api/qcckApi.js";
import * as rule from "@/utils/rules.js";
import { ref, defineExpose, reactive, onMounted, defineEmits, getCurrentInstance, nextTick } from "vue";
const emit = defineEmits(["updateDate"]);
const props = defineProps({
  dic: Object
});
const chooseVisible = ref(false)
const roleIds = ref([])
const { proxy } = getCurrentInstance();
const dialogForm = ref(false); //弹窗
const formData = ref([
  { label: "指令标题", prop: "zlbt", type: "input", },
  { label: "指令类型", prop: "zllx",  type: "select", options: props.dic.D_GS_XS_ZLLX  },
  { label: "指令等级", prop: "zldj",  type: "select", options: props.dic.D_GS_ZDQT_FXDJ },
  { label: "反馈结束时间", prop: "jssj",  type: "datetime"  },
  { label: "联系人", prop: "zllxr",  type: "input"  },
  { label: "联系电话", prop: "zllxdh",  type: "input"  },
  { label: "关联线索", prop: "glxsid",  type: "slot"  },
  { label: "主送单位", prop: "zsdw",  type: "department"  },
  { label: "抄送单位", prop: "csdw",  type: "department"  },
  { label: "指令内容", prop: "zlnr",  type: "textarea",width: '100%' },
  { label: "附件", prop: "fjzd",  type: "upload",width: '100%' },
]);
const listQuery = ref({}); //表单
const loading = ref(false);
const elform = ref();
const title = ref("");
const rules = reactive({
  zlbt: [{ required: true, message: "请输入指令标题", trigger: "blur" }],
  zllx: [{ required: true, message: "请选择指令类型", trigger: "change" }],
  zldj: [{ required: true, message: "请选择指令等级", trigger: "change" }],
  jssj: [{ required: true, message: "请选择反馈结束时间", trigger: "change" }],
  zllxr: [{ required: true, message: "请输入联系人", trigger: "blur" }],
  zllxdh: [{ required: true, message: "请输入联系电话", trigger: "blur" }],
  zsdw: [{ required: true, message: "请选择主送单位", trigger: "change" }],
  csdw: [{ required: true, message: "请选择抄送单位", trigger: "change" }],
  zlnr: [{ required: true, message: "请输入指令内容", trigger: "blur" }],
  glxsid: [{ required: true, message: "请选择关联线索", trigger: "change" }],
});
// 初始化数据
const init = (type, row,) => {
  listQuery.value.fjzd = null
  dialogForm.value = true;
  title.value = type == 'add' ? "新增" : "编辑";
  if (row) getDataById(row.id);
};
// 根据id查询详情
const getDataById = (id) => {
  qcckGet({id}, '/mosty-gsxt/zlxx/selectByid').then((res) => {
    res.fjzd = res.fjzd ? res.fjzd.split(','):[];
    listQuery.value = res;
  });
};

// 选择标签
const choosed = (val) => {
  listQuery.value.glxs = val[0].xsMc;
  listQuery.value.glxsid = val[0].id;
  roleIds.value = val.map(v=>v.id)
}

// 提交
const submit = () => {
  elform.value.submit((data)=>{
    let url =  title.value == "新增" ? '/mosty-gsxt/zlxx/add':'/mosty-gsxt/zlxx/update';
    let params = { ...data }
    loading.value = true;
    params.fjzd = params.fjzd ? params.fjzd.join(','):''
    qcckPost(params, url).then((res) => {
      loading.value = false;
      proxy.$message({ type: "success", message: title.value + "成功" });
      emit("updateDate");
      close();
    }).catch(() => {loading.value = false;});
  });
};

// 关闭
const close = () => {
  listQuery.value = {};
  dialogForm.value = false;
  loading.value = false;
};
defineExpose({ init });
</script>

<style lang="scss" scoped>
@import "~@/assets/css/layout.scss";
@import "~@/assets/css/element-plus.scss";
.box{
  width: 100%;
  height: 32px;
  border-radius: 4px;
  border: 1px solid #e9e9e9;
  padding-left: 10px;
}
</style>
