<?php

namespace model\sample;


class work extends \model
{
    public function list($type,$cid,$where)
    {
        $where = $this->filter2medoo($where);
        $workId = $this->db->get('gsp_adm_work','aw_id',['aw_name' => $type]);
        $data = $this->db->select('gsp_adm_role',['ar_id','ar_name'],$where);
        if(!empty($data)){

            $arId = array_column($data,'ar_id');
            $where = [
                'awr_ar_id' => $arId,
                'awr_aw_id' => $workId
            ];
            if($cid > 0) $where['awr_cid'] = $cid;

            $link = $this->db->select('gsp_adm_work_link_role','awr_ar_id',$where);
            if(!empty($link)){
                foreach ($data as &$value)
                {
                    if(in_array($value['ar_id'],$link))
                    {
                        $value['is_check'] = 1;
                        if($workId == 2 && $cid <= 0){
                            unset($value['is_check']);
                        }
                    }
                }
            }
        }
        return $data;
    }

    /**
     * @param $data
     * @return false|\PDOStatement
     */
    public function operation($data)
    {
        $workId = $this->db->get('gsp_adm_work','aw_id',[
            'aw_name' => $data['aw_name']
        ]);

        if($workId <= 0) return false;

        $arId = $data['ar_id'];

        if(is_string($arId)) $arId = [$arId];

        if($data['statue'] === 'true')
        {
            $addData = [];
            foreach ($arId as $id)
            {
                $addData[] = [
                    'awr_aw_id' => $workId,
                    'awr_ar_id' => $id,
                    'awr_cid' => isset($data['c_id']) ? $data['c_id'] : '0'
                ];
            }
            $this->db->delete('gsp_adm_work_link_role',['awr_aw_id' => $workId,'awr_ar_id' => $arId,'awr_cid' => isset($data['c_id']) ? $data['c_id'] : '0']);
            $result = $this->db->insert('gsp_adm_work_link_role',$addData);
            return $result ? true : false;
        }else{
            $this->db->begin();
            foreach ($arId as $id)
            {
                if (isset($data['c_id'])){
                    $this->db->delete('gsp_adm_work_link_role',[
                        'awr_aw_id' => $workId,
                        'awr_ar_id' => $id,
                        'awr_cid' => isset($data['c_id']) ? $data['c_id'] : '0'
                    ]);
                }
            }
            $result = $this->db->end();
            return true;
        }
    }

    /**
     */
    public function getProline() {
        $where = [
            'level' => 2
        ];
        $rows = $this->db->select('pro_cate', '*', $where);
        return $rows;
    }

    public function prolineSetSave($cateId, $type, $uid)
    {
        $this->db->replace('sample_proline_checkuser', ['splu_proline_id' => $cateId, $type => $uid], ['splu_proline_id']);
    }
    public function getProlineSets($cateId)
    {
        $row =  $this->db->get('sample_proline_checkuser', '*', ['splu_proline_id' => $cateId]);
        $chk1 = $this->db->get('gsp_adm_user', ['au_id', 'au_name'], ['au_id' => $row['splu_chk1_uid']]);
        $chk2 = $this->db->get('gsp_adm_user', ['au_id', 'au_name'], ['au_id' => $row['splu_chk2_uid']]);
        $chk3 = $this->db->get('gsp_adm_user', ['au_id', 'au_name'], ['au_id' => $row['splu_chk3_uid']]);
        $catalog = $this->db->get('gsp_adm_user', ['au_id', 'au_name'], ['au_id' => $row['splu_catalog_uid']]);
        return [
            'chk1' => $chk1['au_name'],
            'chk2' => $chk2['au_name'],
            'chk3' => $chk3['au_name'],
            'catalog' => $catalog['au_name'],
        ];
    }

}
