<template>
    <div class="attendance-config-dialog">
        <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="false" :close-on-press-escape="false">
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="项目名称" prop="projectId">
                    <el-select v-if="mode != 'edit'" v-model="form.projectId" placeholder="请选择项目" clearable
                        @change="doQueryFormSub">
                        <el-option v-for="(item, index) in projectOptions" :key="index" :label="item.projectName"
                            :value="item.id">
                        </el-option>
                    </el-select>
                    <span v-else>{{ form.projectName }}</span>
                </el-form-item>
                <el-form-item label="总包单位" prop="subDeptId">
                    <el-select v-if="mode != 'edit'" v-model="form.subDeptId" placeholder="请选择总包单位" clearable @change="doQueryInfo">
                        <el-option v-for="(item, index) in formDepts" :key="index" :label="item.deptName"
                            :value="item.deptId">
                        </el-option>
                    </el-select>
                    <span v-else>{{ form.unitName }}</span>
                </el-form-item>
                <el-form-item label="接入方式" prop="vendorsCode">
                    <el-select v-if="mode != 'edit'" v-model="form.vendorsCode" placeholder="请选择接入方式" clearable @change="changeRule">
                        <el-option v-if="1==2" key="yanzhu" label="研筑" value="yanzhu"></el-option>
                        <el-option key="gld" label="广联达" value="gld"></el-option>
                        <el-option key="huazhu" label="华筑" value="huazhu"></el-option>
                        <el-option key="jgw" label="济工网" value="jgw"></el-option>
                    </el-select>
                    <span v-else>
                        <span v-if="form.vendorsCode=='yanzhu'">研筑</span>
                        <span v-if="form.vendorsCode=='gld'">广联达</span>
                        <span v-if="form.vendorsCode=='huazhu'">华筑</span>
                        <span v-if="form.vendorsCode=='jgw'">济工网</span>
                    </span>
                    <span>
                        <el-button v-if="mode!='edit'  && form.vendorsCode=='yanzhu'" 
                        :disabled="!form.projectId || !form.subDeptId"
                        type="primary" style="margin-left: 12px;" @click="doMakeAppId">获取</el-button>
                    </span>
                </el-form-item>
                <template v-if="form.vendorsCode=='gld' || form.vendorsCode=='jgw'">
                    <el-form-item label="APPID" prop="appId">
                        <el-input v-model="form.appId" placeholder="请输AppId" clearable />
                    </el-form-item>
                    <el-form-item label="Secret" prop="secret">
                        <el-input v-model="form.secret" placeholder="请输Secret" clearable />
                    </el-form-item>
                    <el-form-item label="项目ID" prop="appProjectId">
                        <el-input v-model="form.appProjectId" placeholder="请输项目ID" clearable />
                    </el-form-item>
                </template>
                <template v-else>
                    <el-form-item label="APPID" prop="appId">
                        <el-input v-model="form.appId" placeholder="请输AppId" clearable />
                    </el-form-item>
                    <el-form-item label="Secret" prop="secret">
                        <el-input v-model="form.secret" placeholder="请输Secret" clearable />
                    </el-form-item>
                    <el-form-item label="项目ID" prop="appProjectId">
                        <el-input v-model="form.appProjectId" placeholder="请输项目ID" clearable />
                    </el-form-item>
                    <el-form-item label="租户ID" prop="appTenantId">
                        <el-input v-model="form.appTenantId" placeholder="请输租户ID" clearable />
                    </el-form-item>
                </template>
                <template v-if="form.vendorsCode=='jgw'">
                    <el-form-item label="项目经理手机" prop="phone">
                        <el-input v-model="form.phone" placeholder="项目经理手机" clearable />
                    </el-form-item>
                </template>
                        <el-form-item label="是否启用" prop="enabled">
                            <el-radio-group v-model="form.enabled">
                                <el-radio :label="1">启用</el-radio>
                                <el-radio :label="0">停用</el-radio>
                            </el-radio-group>
                        </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listAttendanceConfig, getAttendanceConfig, delAttendanceConfig, addAttendanceConfig, updateAttendanceConfig,makeAppId } from "@/api/project/attendanceConfig";
export default {
    dicts: ['attendance_vendors'],
    data() {
        return {
            title: "",
            // 是否显示弹出层
            open: false,
            // 表单参数
            form: {
                projectId: '',
                subDeptId: '',
                appId: '',
                secret: '', 
                vendorsCode:'gld',               
                appProjectId: '',
                appTenantId:'',
                phone:'',
                enabled: 0
            },
            // 表单校验
            rules: {

            },
            projectOptions: [],
            depts: [],
            formDepts: [],
            actTab: '',
            mode: '',
        };
    },

    mounted() {
        this.changeRule();
    },
    methods: {
        doMakeAppId(){
            makeAppId(this.form.projectId,this.form.subDeptId).then(d=>{
                this.form.appId=d.data?.appId||''
                this.form.secret=d.data?.secret||''
                this.form.appProjectId=d.data?.projectId||''
                this.form.appTenantId=d.data?.tenantId||''
            });
        },
        doQueryInfo(){
            if(this.form.projectId && this.form.subDeptId){
                listAttendanceConfig({projectId:this.form.projectId,subDeptId:this.form.subDeptId}).then(d=>{
                    if(d.rows && d.rows.length>0){
                        this.toEdit(d.rows[0]);
                        this.mode="addEdit"
                    }else{
                        this.form.id="";                       
                        this.mode="add";
                        this.form.appId="";
                        this.form.secret="";
                        this.form.appProjectId="";
                        this.form.appTenantId="";
                        this.form.phone="";
                    }
                });

            }else{
                this.form.id="";
                this.mode="add"
                this.form.appId="";
                this.form.secret="";
                this.form.appProjectId="";
                this.form.appTenantId="";
                this.form.phone="";
            }
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    let params = {
                        appId: this.form.appId,
                        secret: this.form.secret,
                        projectId: this.form.appProjectId,
                        tenantId:this.form.appTenantId,
                        phone:this.form.phone
                    };
                    let obj = {
                        projectId: this.form.projectId,
                        subDeptId: this.form.subDeptId,
                        vendorsCode: this.form.vendorsCode,
                        vendorsParameter: JSON.stringify(params),
                        enabled: this.form.enabled,
                        state: 0,
                        isDel: 0
                    };
                    if (this.form.id) {
                        obj.id = this.form.id;
                        updateAttendanceConfig(obj).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.$emit('success');
                        });
                    } else {
                        addAttendanceConfig(obj).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.$emit('success');
                        });
                    }
                }
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        doQueryFormSub() {
            let prjId = this.form.projectId;
            let tmps = this.prjDept2 && this.prjDept2[prjId] ? this.prjDept2[prjId] || [] : [];
            if (tmps.length > 0) {
                this.formDepts = tmps;
                if (tmps.length == 1) {
                    this.form.subDeptId = tmps[0].deptId;
                    this.doQueryInfo();
                } else {
                    this.form.subDeptId = '';
                }
                return;
            }
            this.$api.publics.queryUnitList({
                projectId: prjId,
                unitTypes: "2".split(","),
            }).then((d) => {
                let objs = d.rows || [];
                if (!this.prjDept2) {
                    this.prjDept2 = {};
                }
                this.prjDept2[this.form.projectId] = objs;
                this.formDepts = objs;
                if (objs.length == 1) {
                    this.form.subDeptId = objs[0].deptId;
                    this.doQueryInfo();
                } else {
                    this.form.subDeptId = '';
                }
            });
        },

        reset() {
            this.form = {
                id: null,
                projectId: '',
                subDeptId: '',
                appId: '',
                secret: '',
                vendorsCode:'gld',     
                appProjectId: '',
                appTenantId:'',
                phone:'',
                enabled: 0
            };
            this.resetForm("form");
        },
        changeRule(){
            if(this.form.vendorsCode=='gld'){
                this.rules = {
                    projectId: [{ required: true, trigger: "blur", message: "请选择" }],
                    subDeptId: [{ required: true, trigger: "blur", message: "请选择" }],
                    appId: [{ required: true, trigger: "blur", message: "请输入" }],
                    secret: [{ required: true, trigger: "blur", message: "请输入" }],
                    appProjectId: [{ required: true, trigger: "blur", message: "请输入" }],
                    enabled: [{ required: true, trigger: "blur", message: "请选择" }]
                }
            }else if(this.form.vendorsCode=='jgw'){
                this.rules = {
                    projectId: [{ required: true, trigger: "blur", message: "请选择" }],
                    subDeptId: [{ required: true, trigger: "blur", message: "请选择" }],
                    appId: [{ required: true, trigger: "blur", message: "请输入" }],
                    secret: [{ required: true, trigger: "blur", message: "请输入" }],
                    appProjectId: [{ required: true, trigger: "blur", message: "请输入" }],
                    enabled: [{ required: true, trigger: "blur", message: "请选择" }],
                    phone: [{ required: true, trigger: "blur", message: "输入" }]
                }
            }else{
                this.rules = {
                    projectId: [{ required: true, trigger: "blur", message: "请选择" }],
                    subDeptId: [{ required: true, trigger: "blur", message: "请选择" }],
                    appId: [{ required: true, trigger: "blur", message: "请输入" }],
                    secret: [{ required: true, trigger: "blur", message: "请输入" }],
                    appProjectId: [{ required: true, trigger: "blur", message: "请输入" }],
                    appTenantId: [{ required: true, trigger: "blur", message: "请输入" }],
                    enabled: [{ required: true, trigger: "blur", message: "请选择" }]
                }
            }
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.mode = "add";
            this.title = "添加劳务实名制配置";
            this.changeRule();
            this.actTab = this.dict.type.attendance_vendors[0].value;
            setTimeout(() => {
                this.$refs["form"].clearValidate();
            }, 0);
        },
        showDialog(o) {
            this.projectOptions = o.projectOptions;
            if (o.mode == 'add') {
                this.handleAdd();
            } else {
                this.handleUpdate(o.row);
            }

        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            getAttendanceConfig(id).then(response => {
                this.mode = "edit" 
                this.toEdit( response.data);
            });
        },
        toEdit(obj){                           
                let p = obj.vendorsParameter ? this.$tryToJson(obj.vendorsParameter, {}) : {};
                obj.appId = p.appId || '';
                obj.secret = p.secret || '',
                obj.appProjectId = p.projectId || '';
                obj.appTenantId=p.tenantId||'';
                obj.phone=p.phone||'';
                this.form = obj;
                this.open = true;
                this.title = "修改劳务实名制配置";
                this.actTab = this.dict.type.attendance_vendors[0].value;
                this.changeRule();
                setTimeout(() => {
                    this.$refs["form"].clearValidate();
                }, 400);
        }
    },
};
</script>

<style lang="scss" scoped></style>